這篇文章將為大家詳細講解有關php如何實現爬蟲開發,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

開發一個爬蟲,首先你要知道你的這個爬蟲是要用來做什么的。我是要用來去不同網站找特定關鍵字的文章,并獲取它的鏈接,以便我快速閱讀。
按照個人習慣,我首先要寫一個界面,理清下思路。
1、去不同網站。那么我們需要一個url輸入框。
2、找特定關鍵字的文章。那么我們需要一個文章標題輸入框。
3、獲取文章鏈接。那么我們需要一個搜索結果的顯示容器。
<div class="jumbotron" id="mainJumbotron"> <div class="panel panel-default"> <div class="panel-heading">文章URL抓取</div> <div class="panel-body"> <div class="form-group"> <label for="article_title">文章標題</label> <input type="text" class="form-control" id="article_title" placeholder="文章標題"> </div> <div class="form-group"> <label for="website_url">網站URL</label> <input type="text" class="form-control" id="website_url" placeholder="網站URL"> </div> <button type="submit" class="btn btn-default">抓取</button> </div> </div> <div class="panel panel-default"> <div class="panel-heading">文章URL</div> <div class="panel-body"> <h4></h4> </div> </div> </div>
直接上代碼,然后加上自己的一些樣式調整,界面就完成啦:

那么接下來就是功能的實現了,我用PHP來寫,首先第一步就是獲取網站的html代碼,獲取html代碼的方式也有很多,我就不一一介紹了,這里用了curl來獲取,傳入網站url就能得到html代碼啦:
private function get_html($url){
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
return $html;
}雖然得到了html代碼,但是很快你會遇到一個問題,那就是編碼問題,這可能讓你下一步的匹配無功而返,我們這里統一把得到的html內容轉為utf8編碼:
$coding = mb_detect_encoding($html); if ($coding != "UTF-8" || !mb_check_encoding($html, "UTF-8")) $html = mb_convert_encoding($html, 'utf-8', 'GBK,UTF-8,ASCII');
得到網站的html,要獲取文章的url,那么下一步就是要匹配該網頁下的所有a標簽,需要用到正則表達式,經過多次測試,最終得到一個比較靠譜的正則表達式,不管a標簽下結構多復雜,只要是a標簽的都不放過:(最關鍵的一步)
$pattern = '|<a[^>]*>(.*)</a>|isU'; preg_match_all($pattern, $html, $matches);
匹配的結果在$matches中,它大概是這樣的一個多維素組:
array(2) {
[0]=>
array(*) {
[0]=>
string(*) "完整的a標簽"
.
.
.
}
[1]=>
array(*) {
[0]=>
string(*) "與上面下標相對應的a標簽中的內容"
}
}只要能得到這個數據,其他就完全可以操作啦,你可以遍歷這個素組,找到你想要a標簽,然后獲取a標簽相應的屬性,想怎么操作就怎么操作啦,下面推薦一個類,讓你更方便操作a標簽:
$dom = new DOMDocument();
@$dom->loadHTML($a);//$a是上面得到的一些a標簽
$url = new DOMXPath($dom);
$hrefs = $url->evaluate('//a');
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href'); //這里獲取a標簽的href屬性
}當然,這只是一種方式,你也可以通過正則表達式匹配你想要的信息,把數據玩出新花樣。
得到并匹配得出你想要的結果,下一步當然就是傳回前端將他們顯示出來啦,把接口寫好,然后前端用js獲取數據,用jquery動態添加內容顯示出來:
var website_url = '你的接口地址';
$.getJSON(website_url,function(data){
if(data){
if(data.text == ''){
$('#article_url').html('<div><p>暫無該文章鏈接</p></div>');
return;
}
var string = '';
var list = data.text;
for (var j in list) {
var content = list[j].url_content;
for (var i in content) {
if (content[i].title != '') {
string += '<div class="item">' +
'<em>[<a href="http://' + list[j].website.web_url + '" target="_blank">' + list[j].website.web_name + '</a>]</em>' +
'<a href=" ' + content[i].url + '" target="_blank" class="web_url">' + content[i].title + '</a>' +
'</div>';
}
}
}
$('#article_url').html(string);
});上最終效果圖:


關于“php如何實現爬蟲開發”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網站標題:php如何實現爬蟲開發-創新互聯
新聞來源:http://www.js-pz168.com/article30/dhdipo.html
成都網站建設公司_創新互聯,為您提供服務器托管、網頁設計公司、網站導航、App開發、定制開發、用戶體驗
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯