perl HTML::LinkExtor模組(1)

來源:互聯網
上載者:User

標籤:color   blank   check   link   each   htm   模組   簽名   com   

 1 use LWP::Simple; 2 use HTML::LinkExtor; 3  4 $html = get("http://www.baidu.com"); 5 $link = HTML::LinkExtor->new(\&check); 6 $link->parse($html); 7  8 sub check{ 9     ($tag, %links) = @_;10     print "$tag\n";11     foreach $key(keys %links){12         print "$key -> $links{$key}\n";13     }14 }15 16 #$tag為標籤類型, 如a, link, img, script等17 #%links為hash類型, 鍵為連結名,值為連結值18 #比如對於a標籤, links中的key為href, 值為href中的連結名19 # link20 # href -> /favicon.ico21 # link22 # href -> /content-search.xml23 # link24 # href -> //www.baidu.com/img/baidu.svg25 # link26 # href -> //s1.bdstatic.com27 # link28 # href -> //t1.baidu.com29 # link30 # href -> //t2.baidu.com31 # link32 # href -> //t3.baidu.com33 # link34 # href -> //t10.baidu.com35 # link36 # href -> //t11.baidu.com37 # link38 # href -> //t12.baidu.com39 # link40 # href -> //b1.bdstatic.com41 # img42 # src -> //www.baidu.com/img/bd_logo1.png

這個代碼列印頁面中的所有標籤名與對應的link連結地址

如果我們要列印其中的所有img地址呢,那我們可能用$tag來判斷是哪種標籤, 從而再進一步提取資料

具體可以看這裡: perl HTML::LinkExtor模組(2)

 

perl HTML::LinkExtor模組(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.