perl,获取url 中的链接

上一篇 / 下一篇  2012-04-24 23:47:55 / 个人分类:perl相关

 

有个模块,HTML::LinkExtor可以实现获取网页内容中的链接。

其他还有一些模块也有类似的功能。

 

下面是示例代码:

#!/usr/bin/perl

use HTML::LinkExtor;
use LWP::Simple;

$base_url = shift;
$parser = HTML::LinkExtor->new(undef,$base_url);
$parser->parse(get($base_url))->eof;
@links = $parser->links;
foreach $linkarray (@links) {
 my @element = @$linkarray;
 my $elt_type = shift @element;
 while( @element) {
  my ($attr_name , $attr_value) = splice(@element, 0, 2);
  $seen{$attr_value}++;
 }
}
for (sort keys %seen) { print $_, "\n" }

运行结果,查看下google主页

D:\loveperl>perl xurl.pl http://www.google.com.hk
http://maps.google.com.hk/maps?hl=zh-TW&tab=wl
http://news.google.com.hk/nwshp?hl=zh-TW&tab=wn
http://translate.google.com.hk/?hl=zh-TW&tab=wT
http://video.google.com.hk/?hl=zh-TW&tab=wv
http://www.google.com.hk/advanced_search?hl=zh-TW
http://www.google.com.hk/history/optout?hl=zh-TW
http://www.google.com.hk/imghp?hl=zh-TW&tab=wi
http://www.google.com.hk/intl/zh-TW/about.html
http://www.google.com.hk/intl/zh-TW/ads/
http://www.google.com.hk/intl/zh-TW/options/
http://www.google.com.hk/intl/zh-TW/policies/
http://www.google.com.hk/language_tools?hl=zh-TW
http://www.google.com.hk/logos/2012/sundback12-hp.jpg
http://www.google.com.hk/preferences?hl=zh-TW
http://www.google.com.hk/search
http://www.google.com.hk/search?q=Gideon+Sundback&ct=sundback12-hp&oi=ddle&hl=zh
-TW
http://www.google.com.hk/setprefs?sig=0_aIbe4rMX9LOKPm45Qkzk3EmDqe4%3D&hl=en
http://www.google.com.hk/setprefs?sig=0_aIbe4rMX9LOKPm45Qkzk3EmDqe4%3D&hl=zh-CN
http://www.google.com.hk/url?sa=p&pref=ig&pval=3&q=http://www.google.com.hk/ig%3
Fhl%3Dzh-TW%26source%3Diglk&usg=AFQjCNHPiYicHSGMua-uVhGTjPT-hWpWXA
http://www.google.com/ncr
https://accounts.google.com/ServiceLogin?hl=zh-TW&continue=http://www.google.com
.hk/
https://mail.google.com/mail/?tab=wm


TAG:

 

评分:0

我来说两句

Open Toolbar