它从一开始只输出几行。

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
my $response = $ua->get('http://www.eurogamer.net/articles/df-hardware-wii-u-graphics-power-finally-revealed');
print $response->decoded_content;

最佳答案

我进行了以下修改:

my $response = $ua->get( 'http://www.eurogamer.net/articles/df-hardware-wii-u-graphics-power-finally-revealed' );
say $response->headers->as_string;

并看到了这一点:
Cache-Control: max-age=60s
Connection: close
Date: Wed, 06 Feb 2013 23:51:15 GMT
Via: 1.1 varnish
Age: 0
Server: Apache
Vary: Accept-Encoding
Content-Length: 50519
Content-Type: text/html; charset=ISO-8859-1
Client-Aborted: die
Client-Date: Wed, 06 Feb 2013 23:50:50 GMT
Client-Peer: 94.198.83.18:80
Client-Response-Num: 1
X-Died: Illegal field name 'X-Meta-Twitter:card' at .../HTML/HeadParser.pm line 207.
X-Varnish: 630361704

它似乎不喜欢第 27 行的 <meta name="twitter:card" content="summary" /> 标签。它说它死了。

它似乎将具有 meta 属性的任何 name 标记转换为 "X-Meta-\u$attr->{name}" “标题”。然后它尝试将 content 属性的值存储为 X-meta “header” 值。像这样(从第 194 行开始):
if ($tag eq 'meta') {
    my $key = $attr->{'http-equiv'};
    if (!defined($key) || !length($key)) {
        if ($attr->{name}) {
            $key = "X-Meta-\u$attr->{name}"; # <-- Here's the little trick
        } elsif ($attr->{charset}) { # HTML 5 <meta charset="...">
            $key = "X-Meta-Charset";
            $self->{header}->push_header($key => $attr->{charset});
            return;
        } else {
            return;
        }
    }
    $self->{'header'}->push_header($key => $attr->{content});
}

我将此模块的修改副本推送到 PERL5LIB 目录中。我将 push_header 步骤包装在一个 eval 块中并完全下载了页面。

关于perl - 为什么 LWP::UserAgent 不能完全获取这个站点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14740365/

10-13 07:29