正如标题所说,WWW::Mechanize 无法识别
<base href="" />
如果页面内容被压缩。这是一个例子:
use strict;
use warnings;
use WWW::Mechanize;
my $url = 'http://objectmix.com/perl/356181-help-lwp-log-after-redirect.html';
my $mech = WWW::Mechanize->new;
$mech->get($url);
print $mech->base()."\n";
# force plain text instead of gzipped content
$mech->get($url, 'Accept-Encoding' => 'identity');
print $mech->base()."\n";
输出:
http://objectmix.com/perl/356181-help-lwp-log-after-redirect.html
http://objectmix.com/ <--- this is correct !
我在这里错过了什么吗?谢谢
编辑:我刚刚用 LWP::UserAgent 直接测试了它,它没有任何问题:
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $res = $ua->get('http://objectmix.com/perl/356181-help-lwp-log-after-redirect.html');
print $res->base()."\n";
输出:
http://objectmix.com/
这看起来像 WWW::Mechanize 错误?
编辑 2:这是 LWP 或 HTTP::Response 错误,而不是 WWW::Mechanize。LWP 默认不请求 gzip。如果我设置
$ua->default_header('Accept-Encoding' => 'gzip'),
在上面的示例中,它返回错误的基数
编辑 3: 错误在 parse_head() 的 LWP/UserAgent.pm 中
它使用 gzip 压缩的 HTML 调用 HTML/HeadParser,而 HeadParser 不知道如何处理它。LWP 应该在调用解析子例程之前对内容进行压缩。