访问 WWW::Mechanize 响应的首选方法是什么?
这两个版本都可以吗,还是更喜欢其中一个?
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $content;
# 1
$mech->get( 'http://www.kernel.org' );
$content = $mech->content;
print $content;
# 2
my $res = $mech->get( 'http://www.kernel.org' );
$content = $res->content;
print $content;
Are both of these versions OK or is one of them to prefer?
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $content;
# 1
$mech->get( 'http://www.kernel.org' );
$content = $mech->content;
print $content;
# 2
my $res = $mech->get( 'http://www.kernel.org' );
$content = $res->content;
print $content;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们都是可以接受的。第二个对我来说似乎更干净,因为它返回一个正确的 HTTP::Response 对象您可以在其上查询和调用方法,也意味着如果您进行另一个 Mechanize 请求,您仍然可以访问旧的 HTTP 响应。使用第一种方法时,每次发出请求时,
content
方法都会更改为新的内容,这听起来很容易出错。顺便说一句,无论哪种方法,您都应该在访问内容之前检查
$response->is_success
或$mech->success
,因为请求可能失败。They are both acceptable. The second one seems cleaner to me because it returns a proper HTTP::Response object which you can query and call methods on, and also means that if you make another Mechanize request, you'll still have access to the old HTTP response. With your first approach, each time you make a request, the
content
method will change to something new, which sounds error-prone.Btw, for either method, you should check
$response->is_success
or$mech->success
before accessing the content, as the request may have failed.内容( ) 方法有时更方便:
返回机械内部用于最后获取的页面的内容。通常这与 $mech->response()->content() 相同,但是如果“update_html”重载,和/或额外的命名参数传递给 content(),则这对于 HTML 文档可能有所不同
:页面的纯文本版本,删除了所有 HTML 标记。此功能需要安装 HTML::TreeBuilder,否则将引发致命错误。
返回经过修改以在标头中包含标记的 HTML 文档。如果未指定,$base_href 为 $mech->base()。这可以方便地将 HTML 传递给 HTML::Display。
The content() method is sometimes more convenient:
Returns the content that the mech uses internally for the last page fetched. Ordinarily this is the same as $mech->response()->content(), but this may differ for HTML documents if "update_html" is overloaded, and/or extra named arguments are passed to content():
Returns a text-only version of the page, with all HTML markup stripped. This feature requires HTML::TreeBuilder to be installed, or a fatal error will be thrown.
Returns the HTML document, modified to contain a mark-up in the header. $base_href is $mech->base() if not specified. This is handy to pass the HTML to e.g. HTML::Display.
$mech->content 是专门存在的,因此您可以不必获取结果响应。越简单越好。
$mech->content is specifically there so you can bypass having to get the result response. The simpler it is, the better.