簡單的php爬蟲案例是什麼呢?

來源:互聯網
上載者:User
本人php新手,尤其是對php運用極差。
有沒有來一段php爬蟲的簡單案例,激發新手對php熱愛的慾望。
比如用php爬某個網站的資料庫?
請問如何用php不同的方法和不一樣的函數或者用正則比。用file還是什麼的?

回複內容:

本人php新手,尤其是對php運用極差。
有沒有來一段php爬蟲的簡單案例,激發新手對php熱愛的慾望。
比如用php爬某個網站的資料庫?
請問如何用php不同的方法和不一樣的函數或者用正則比。用file還是什麼的?

https://github.com/search?utf8=%E2%9C%93&q=php+crawler

能好好提問嗎?
誰告訴你能爬別人網站的資料庫?資料!=資料庫.
擷取HTML內容的方法可以用file_get_contents(),curl,fopen,fsockopen,etc

最簡單的:擷取segmentfault首頁內容:

echo file_get_contents('https://segmentfault.com/');

HTML內容提取

可能你需要對頁面內容進行提取,可以用Regex,但是不建議這麼用,一旦網站改版很難維護,或者html內容不規則,

建議使用phpquery這樣的DOM解析,有國人基於phpquery開發的querylist

find('article.newsentry') as $article) {    $item['time']    = trim($article->find('time',            0)->plaintext);    $item['title']   = trim($article->find('h2.newstitle',    0)->plaintext);    //$item['content'] = trim($article->find('div.newscontent', 0)->plaintext);    $news[] = $item;}print_r($news);

比如上面就是用Simple HTML DOM這個PHP的DOM分析庫採集php.net的首頁新聞,可以很方便地像jQuery那樣進行DOM操作,擷取HTML裡需要的資料.
http://simplehtmldom.sourceforge.net/manual.htm

  • 相關文章

    聯繫我們

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