網頁抓取:PHP實現網頁爬蟲方式小結,抓取爬蟲_PHP教程

來源:互聯網
上載者:User

網頁抓取:PHP實現網頁爬蟲方式小結,抓取爬蟲


來源:http://www.ido321.com/1158.html

抓取某一個網頁中的內容,需要對DOM樹進行解析,找到指定節點後,再抓取我們需要的內容,過程有點繁瑣。LZ總結了幾種常用的、易於實現的網頁抓取方式,如果熟悉JQuery選取器,這幾種架構會相當簡單。

一、Ganon

項目地址: http://code.google.com/p/ganon/

文檔: http://code.google.com/p/ganon/w/list

測試:抓取我的網站首頁所有class屬性值是focus的div元素,並且輸出class值

include 'ganon.php'; $html = file_get_dom('http://www.ido321.com/'); foreach($html('div[class="focus"]') as $element) {   echo $element->class, "
\n"; }?>

結果:

二、phpQuery

項目地址:http://code.google.com/p/phpquery/

文檔:https://code.google.com/p/phpquery/wiki/Manual

測試:抓取我網站首頁的article標籤元素,然後出書其下h2標籤的html值

include 'phpQuery/phpQuery.php'; phpQuery::newDocumentFile('http://www.ido321.com/'); $artlist = pq("article"); foreach($artlist as $title){    echo pq($title)->find('h2')->html()."
"; } ?>

結果:

三、Simple-Html-Dom

項目地址: http://simplehtmldom.sourceforge.net/
文檔: http://simplehtmldom.sourceforge.net/manual.htm

測試:抓取我網站首頁的所有連結

include 'simple_html_dom.php';//使用url和file都可以建立DOM$html = file_get_html('http://www.ido321.com/');//找到所有圖片// foreach($html->find('img') as $element)//        echo $element->src . '
';//找到所有連結foreach($html->find('a') as $element) echo $element->href . '
'; ?>

結果:(是一部分)

四、Snoopy

項目地址:http://code.google.com/p/phpquery/

文檔:http://code.google.com/p/phpquery/wiki/Manual

測試:抓取我的網站首頁

include("Snoopy.class.php");$url = "http://www.ido321.com";$snoopy = new Snoopy;$snoopy->fetch($url); //擷取所有內容 echo $snoopy->results; //顯示結果// echo $snoopy->fetchtext ;//擷取常值內容(去掉html代碼)// echo $snoopy->fetchlinks($url) ;//擷取連結// $snoopy->fetchform ;//擷取表單 ?>

結果:

五、手動編寫爬蟲

如果編寫能力ok,可以手寫一個網頁爬蟲,實現網頁抓取。網上有千篇一律的介紹此方法的文章,LZ就不贅述了。有興趣瞭解的,可以百度 php 網頁抓取。

ps:資源分享

常見的開源爬蟲項目請戳:http://blog.chinaunix.net/uid-22414998-id-3774291.html

下一篇:國民嶽父的“屁民理論”




php網路爬蟲實現採集某個網站的部分內容

樓主,你可以使用simpl_html_dom 這個類來採集,具體怎麼使用,如果你會jquery的話,相信你看一下就懂了。祝你好運。
 

爬蟲抓取網頁關鍵字、摘要以供搜尋

strip_tags($string)
 

http://www.bkjia.com/PHPjc/907659.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/907659.htmlTechArticle網頁抓取:PHP實現網頁爬蟲方式小結,抓取爬蟲 來源:http://www.ido321.com/1158.html 抓取某一個網頁中的內容,需要對DOM樹進行解析,找到指定...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.