本人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