perl - 使用组合脚本解析错误

时间:2018-04-25 19:02:56

标签: perl parsing html-parsing

当我使用这个脚本时,它的工作正常 我使用HTML :: TreeBuilder来解析网站上的html

#!/usr/bin/perl -w
use LWP::Simple;
use HTML::TreeBuilder;

use open ':std', ':encoding(UTF-8)';
my $base='https://myanimelist.net';
my $url="/anime/35849/Darling_in_the_FranXX";
my $page = get($base.$url) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );

my @trips= $p->look_down(_tag=>'span',itemprop=>'description')->as_text;
foreach my $trip (@trips){
   print $trip;
}

但是当我将它添加到这个脚本时它不起作用

#!/usr/bin/perl -w
use LWP::Simple;
use HTML::TreeBuilder;
my $ua = LWP::UserAgent->new();


my $mal = "https://myanimelist.net/search/all?q=darling";
my $response = $ua->request(HTTP::Request->new(GET => $mal));
my $rrs = $response->content;
while ($rrs =~ m/href=\"https:\/\/myanimelist.net\/anime\/(.*?)\" class=\"hoverinfo_trigger fw-b fl-l\"/g){
my $link = $1;
if ( $link !~ /video|season?_location=mal_h_m/ ) {
print ("https://myanimelist.net/$link\n");

use open ':std', ':encoding(UTF-8)';
my $base='https://myanimelist.net';
$fullink = "$link";
my $page = get($base.$fullink) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );

my @trips= $p->look_down(_tag=>'span',itemprop=>'description')->as_text;
foreach my $trip (@trips){
   print $trip;
}
}
}

我收到此错误代码:

Invalid argument at name.pl line 19.

有没有办法解决它?

1 个答案:

答案 0 :(得分:3)

这里有问题。看起来你错过了动漫:

my $page = get($base.$fullink) or die $!;

尝试将其替换为:

my $url = $base.'/anime/'.$fullink;
my $page = get($url) or die $!;

或替换:

my $base='https://myanimelist.net';

my $base='https://myanimelist.net/anime/';