有時候網頁所連結的內容不見了,除非有人告訴你,否則還真懶得去找,對維護網頁的人來說,應該都會有這樣的一個工具,就是利用程式去幫你找找那個連結的網頁不見,現在我們也來實作一個。
想法是這樣的,先把網頁抓下來,解析網頁內容看看有沒有a標簽,有的話就試著連連看網頁是否還存在。

程式碼
use LWP::Simple;
use HTML::LinkExtor;

my $url="http://www.pixnet.net/";
my $doc=get($url) or die;
my $extor=HTML::LinkExtor->new(undef, $url);
$extor->parse($doc) or die;
my @links=$extor->links;
foreach my $alink(@links){
my $tag=shift @$alink;
next if($tag !~ /a/i);
my %attrs=@$alink;
while(my($attr, $aurl)=(each %attrs)){
next unless $attr eq 'href' && $aurl =~ /^http:/;
if(head($aurl)){
print "$aurl Yes.\n";
}else{
print "$aurl No.\n";
}
}
}

解說
本程式以pixnet首頁為例,先以LWP::Simple模組的get方法抓取網頁(參考利用LWP::Simple下載網頁),再以HTML::LinkExtor來解析HTML的內容,我們有興趣的是連結部份,解出連結後再判斷是否為a標簽,如果是再判斷是否有href屬性及連結是否為http:開頭,都符合的話再叫用LWP::Simple模組的head方法測試連結是否存在。
這個程式只實作了一個頁面的抓取方法,當然你可以弄個迴圈,這樣他就可以抓很多了。
我在另外一篇用了另一個模組HTML::TreeBuilder來解析標簽,有興趣也可看看。(參考抓取pixnet裡的文章連結)

成果
檢查結果,似乎都沒有錯的 :)
http://www.pixnet.net/ Yes.
http://www.pixnet.net/indexalbum/hot Yes.
http://www.pixnet.net/indexblog/hotarticle Yes.
http://support.pixnet.tw/ Yes.
http://www.pixnet.net/register.php Yes.
http://www.pixnet.net/direct.php?aid=703 Yes.
http://www.pixnet.net/direct.php?aid=594 Yes.
http://www.pixnet.net/direct.php?aid=703 Yes.
http://www.pixnet.net/direct.php?aid=703 Yes.
http://www.pixnet.net/direct.php?aid=703 Yes.
http://www.pixnet.net/forget_password.php Yes.
http://www.pixnet.net/register.php Yes.
http://www.pixnet.net/search/Wii Yes.
http://www.pixnet.net/search/%E5%BD%A9%E5%A6%9D Yes.
http://www.pixnet.net/search/%E7%8E%8B%E5%BB%BA%E6%B0%91 Yes.
http://www.pixnet.net/search/%E7%BE%8E%E9%A3%9F Yes.
http://www.pixnet.net/direct.php?aid=417 Yes.
http://www.pixnet.net/direct.php?aid=526 Yes.
http://www.pixnet.net/direct.php?aid=532 Yes.
http://www.pixnet.net/direct.php?aid=647 Yes.
http://www.pixnet.net/home/cwyuni Yes.
http://www.pixnet.net/album/cwyuni/1223241 Yes.
http://www.pixnet.net/album/sgdyang/1650997 Yes.
http://www.pixnet.net/photo/jiazha/53678915 Yes.
http://www.pixnet.net/photo/lovemily/46927440 Yes.
http://www.pixnet.net/direct.php?aid=680 Yes.
http://www.pixnet.net/album/atallevents/1591113 Yes.
http://www.pixnet.net/home/atallevents Yes.
http://www.pixnet.net/album/atallevents/1591113 Yes.
http://www.pixnet.net/album/maktub8/1661302 Yes.
http://www.pixnet.net/home/maktub8 Yes.
http://www.pixnet.net/album/maktub8/1661302 Yes.
http://www.pixnet.net/album/legato5703/1666376 Yes.
http://www.pixnet.net/home/legato5703 Yes.
http://www.pixnet.net/album/legato5703/1666376 Yes.
http://www.pixnet.net/album/yuri1030/1666142 Yes.
http://www.pixnet.net/home/yuri1030 Yes.
http://www.pixnet.net/album/yuri1030/1666142 Yes.
http://www.pixnet.net/album/vivipig7897/1489823 Yes.
http://www.pixnet.net/home/vivipig7897 Yes.
http://www.pixnet.net/album/vivipig7897/1489823 Yes.
http://www.pixnet.net/indexalbum/hot Yes.
http://www.pixnet.net/album/sweet14299/1630846 Yes.
http://www.pixnet.net/home/sweet14299 Yes.
http://www.pixnet.net/album/sweet14299/1630846 Yes.
http://www.pixnet.net/album/Nuances/1665046 Yes.
http://www.pixnet.net/home/Nuances Yes.
http://www.pixnet.net/album/Nuances/1665046 Yes.
http://www.pixnet.net/album/sasaka/1091188 Yes.
http://www.pixnet.net/home/sasaka Yes.
http://www.pixnet.net/album/sasaka/1091188 Yes.
http://www.pixnet.net/album/solla/1647993 Yes.
http://www.pixnet.net/home/solla Yes.
http://www.pixnet.net/album/solla/1647993 Yes.
http://www.pixnet.net/album/aboutmakoto/1658050 Yes.
http://www.pixnet.net/home/aboutmakoto Yes.
http://www.pixnet.net/album/aboutmakoto/1658050 Yes.
http://www.pixnet.net/indexalbum/dhot Yes.
http://blog.pixnet.net/ntakahashi0419 Yes.
http://blog.pixnet.net/ntakahashi0419 Yes.
http://blog.pixnet.net/post/ntakahashi0419/4883134 Yes.
http://blog.pixnet.net/princeemi Yes.
http://blog.pixnet.net/princeemi Yes.
http://blog.pixnet.net/post/princeemi/4591167 Yes.
http://blog.pixnet.net/cyndi124950 Yes.
http://blog.pixnet.net/cyndi124950 Yes.
http://blog.pixnet.net/post/cyndi124950/3354010 Yes.
http://blog.pixnet.net/justbird Yes.
http://blog.pixnet.net/justbird Yes.
http://blog.pixnet.net/post/justbird/4899772 Yes.
http://blog.pixnet.net/eastyle2007 Yes.
http://blog.pixnet.net/eastyle2007 Yes.
http://blog.pixnet.net/post/eastyle2007/4746061 Yes.
http://blog.pixnet.net/minstrel Yes.
http://blog.pixnet.net/minstrel Yes.
http://blog.pixnet.net/post/minstrel/4889565 Yes.
http://www.pixnet.net/indexblog/hotarticle Yes.
http://www.pixnet.net/indexalbum/1 Yes.
http://www.pixnet.net/indexalbum/2 Yes.
http://www.pixnet.net/indexalbum/3 Yes.
http://www.pixnet.net/indexalbum/4 Yes.
http://www.pixnet.net/indexalbum/5 Yes.
http://www.pixnet.net/indexalbum/6 Yes.
http://www.pixnet.net/indexalbum/7 Yes.
http://www.pixnet.net/indexalbum/8 Yes.
http://www.pixnet.net/indexalbum/9 Yes.
http://www.pixnet.net/indexalbum/10 Yes.
http://www.pixnet.net/indexalbum/11 Yes.
http://www.pixnet.net/indexalbum/1 Yes.
http://pixsong.pixnet.net Yes.
http://www.pixnet.net/indexblog/1 Yes.
http://www.pixnet.net/indexblog/2 Yes.
http://www.pixnet.net/indexblog/3 Yes.
http://www.pixnet.net/indexblog/4 Yes.
http://www.pixnet.net/indexblog/5 Yes.
http://www.pixnet.net/indexblog/6 Yes.
http://www.pixnet.net/indexblog/7 Yes.
http://www.pixnet.net/indexblog/8 Yes.
http://www.pixnet.net/indexblog/20 Yes.
http://www.pixnet.net/indexblog/13 Yes.
http://www.pixnet.net/indexblog/1 Yes.
http://blog.pixnet.net/admin/listall/1 Yes.
http://blog.pixnet.net/post/admin/4883125 Yes.
http://blog.pixnet.net/post/admin/4840413 Yes.
http://blog.pixnet.net/post/admin/4831129 Yes.
http://blog.pixnet.net/post/admin/4809875 Yes.
http://blog.pixnet.net/post/admin/4795195 Yes.
http://blog.pixnet.net/post/admin/4680128 Yes.
http://blog.pixnet.net/post/admin/4662846 Yes.
http://blog.pixnet.net/post/admin/4657804 Yes.
http://blog.pixnet.net/post/admin/4638560 Yes.
http://blog.pixnet.net/post/admin/4634750 Yes.
http://www.pixnet.net/direct.php?aid=633 Yes.
http://blog.pixnet.net/admin/category/list/110677 Yes.
http://blog.pixnet.net/post/admin/4831129 Yes.
http://blog.pixnet.net/post/admin/4809875 Yes.
http://blog.pixnet.net/post/admin/4680128 Yes.
http://blog.pixnet.net/post/admin/4662846 Yes.
http://blog.pixnet.net/post/admin/4657804 Yes.
http://blog.pixnet.net/post/admin/4638560 Yes.
http://blog.pixnet.net/post/admin/4634750 Yes.
http://blog.pixnet.net/post/admin/4529922 Yes.
http://blog.pixnet.net/post/admin/3984444 Yes.
http://blog.pixnet.net/post/admin/3841061 Yes.
http://www.pixnet.net/direct.php?aid=656 Yes.
http://support.pixnet.tw/ Yes.
http://support.pixnet.tw/index.php?topic=17228.msg47385#msg47385 Yes.
http://support.pixnet.tw/index.php?topic=17227.msg47383#msg47383 Yes.
http://support.pixnet.tw/index.php?topic=17225.msg47381#msg47381 Yes.
http://support.pixnet.tw/index.php?topic=17224.msg47380#msg47380 Yes.
http://support.pixnet.tw/index.php?topic=17223.msg47373#msg47373 Yes.
http://support.pixnet.tw/index.php?topic=17222.msg47371#msg47371 Yes.
http://support.pixnet.tw/index.php?topic=17221.msg47370#msg47370 Yes.
http://support.pixnet.tw/index.php?topic=17220.msg47364#msg47364 Yes.
http://support.pixnet.tw/index.php?topic=17219.msg47359#msg47359 Yes.
http://support.pixnet.tw/index.php?topic=17218.msg47357#msg47357 Yes.
http://support.pixnet.tw/index.php?topic=17217.msg47354#msg47354 Yes.
http://support.pixnet.tw/index.php?topic=17216.msg47351#msg47351 Yes.
http://support.pixnet.tw/index.php?topic=17215.msg47349#msg47349 Yes.
http://www.pixnet.net/register.php Yes.
http://www.pixnet.net/regulations.php Yes.
http://support.pixnet.tw/index.php?board=18.0 Yes.
http://blog.pixnet.net/EMKT/post/4155826 Yes.
http://blog.pixnet.net/EMKT Yes.
創作者介紹

神鵰蝦

jck11 發表在 痞客邦 PIXNET 留言(1) 人氣()


留言列表 (1)

發表留言
  • mini1
  • 這是什麼東東?不是我這個凡人看得懂得的,我想,我還是看笑話就可以了。Good day!
  • 有興趣可以玩玩,沒興趣就當他是灌水文吧 :) Have a nice day.

    jck11 於 2007/05/28 20:19 回覆