Plain Text
复制代码1
2
3
foreach my $img (@images) {
print "图片链接:$img\\n";
}
Plain Text
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
复制
#!/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";
}
若有收获,就点个赞吧