我试图使用Perl5和LWP :: UserAgent访问一个站点。 但是在连接时,脚本会以“403 access denied”消息消失。 奇怪的是它使用WWW :: Mechanize完美无缺,但获取代码完全相同。 通常我会怀疑用户代理是原因,但正如前面提到的那样,代码在两种情况下都是相同的。
WWW :: Mechanize和LWP :: UserAgent处理可能导致此问题的请求有何不同?
以下是一些演示两种不同方法的示例代码。
# Mechanize use strict; use warnings "all"; use WWW::Mechanize; my $mech = WWW::Mechanize->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $mech->get("www.foo.com"); # LWP use strict; use warnings "all"; use LWP::UserAgent; my $ua = LWP::UserAgent->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $r = $ua->get("www.foo.com");I am trying to access a site using Perl5 and LWP::UserAgent. However upon connecting, the script dies with a "403 access denied" message. The weird part is that it works flawlessly using WWW::Mechanize but the fetch code is the exact same. Normally I'd suspect the user agent being the cause but as mentioned before the code is the same in both cases.
Is there a difference in how WWW::Mechanize and LWP::UserAgent handle requests that could cause this issue?
Here is some sample code that demonstrates two different approaches.
# Mechanize use strict; use warnings "all"; use WWW::Mechanize; my $mech = WWW::Mechanize->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $mech->get("www.foo.com"); # LWP use strict; use warnings "all"; use LWP::UserAgent; my $ua = LWP::UserAgent->new( agent_alias => 'Mozilla/5.0', show_progress => 1); my $r = $ua->get("www.foo.com");最满意答案
对于LWP :: UserAgent和WWW :: Mechanize,都没有记录agent_alias参数。 而且也没有agent_alias参数实现。 相反, 在两种情况下都会忽略该参数 ,并使用内置默认值 。 但默认是不同的 。 对于WWW :: Mechanize,有一个agent_alias 方法 。 从文档 :
...例如,
$mech->agent_alias( 'Windows IE 6' );将您的用户代理设置为
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
根据LWP :: UserAgent的文档,您实际想要使用的参数被正确地称为agent ,默认为libwww-perl/#.###是版本号)。 使用WWW :: Mechanize可以使用相同的参数,但记录的不同默认值是WWW-Mechanize/#.## 。
There is no agent_alias argument documented, neither for LWP::UserAgent nor for WWW::Mechanize. And there is also no agent_alias argument implement. Instead the argument gets ignored in both cases and it uses the builtin default. But the default is different. There is a agent_alias method though for WWW::Mechanize. From the documentation:
...For instance,
$mech->agent_alias( 'Windows IE 6' );sets your User-Agent to
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
According to the documentation of LWP::UserAgent the argument you actually want to use is properly called agent and defaults to libwww-perl/#.### (the #.### being the version number). With WWW::Mechanize the same argument can be used but the documented different default is WWW-Mechanize/#.##.
更多推荐
发布评论