eval {
# 解析HTML的代码
};
if ($@) {
warn "解析HTML时发生错误:$@";
}
foreach my $img (@images) {
print "图片链接:$img\n";
}
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTML::Parser;
my $ua = LWP::UserAgent->new;
my $url = 'http://www.example.com';
my $response = $ua->get($url);
if (!$response->is_success) {
die "请求失败,状态码:" . $response->code . ",消息:" . $response->message;
}
my $html = $response->decoded_content;
my $parser = HTML::Parser->new(api_version => 3.14);
my @images;
$parser->handler(start => sub { push @images, $_[0]{data} if $_[0]{tag} eq 'img' });
eval {
$parser->parse($html);
};
if ($@) {
warn "解析HTML时发生错误:$@";
exit;
}
foreach my $img (@images) {
print "图片链接:$img\n";
}
若有收获,就点个赞吧