加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl网页爬虫

发布时间:2020-12-16 00:02:07 所属栏目:大数据 来源:网络整理
导读:这代码还没来的急分析,这几天忙一些事情,先mark下官方的代码,等忙完这几天我就好好看看! # !/usr/bin/perl use LWP:: UserAgent; use HTML:: LinkExtor; use URI:: URL; $url = " http://www.163.com " ; # for instance $ua = LWP::UserAgent- new; # S

这代码还没来的急分析,这几天忙一些事情,先mark下官方的代码,等忙完这几天我就好好看看!

#!/usr/bin/perl

use LWP::UserAgent; use HTML::LinkExtor; use URI::URL; $url = "http://www.163.com";  # for instance
$ua = LWP::UserAgent->new; # Set up a callback that collect image links
my @imgs = (); sub callback { my($tag,%attr) = @_; return if $tag ne 'a';  # we only look closer at <img ...>
     push(@imgs,values %attr); } # Make the parser. Unfortunately,we don't know the base yet # (it might be different from $url)
$p = HTML::LinkExtor->new(&;callback); # Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),sub {$p->parse($_[0])}); # Expand all image URLs to absolute ones
my $base = $res->base; @imgs = map { $_ = url($_,$base)->abs; } @imgs; # Print them out
print join("n",@imgs),"n";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读