《PHP實(shí)戰(zhàn):php實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的開(kāi)發(fā)》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):php實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的開(kāi)發(fā),希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
有時(shí)候因?yàn)楣ぷ鳌⒆陨淼男枨?我們都會(huì)去瀏覽不同網(wǎng)站去獲取我們需要的數(shù)據(jù),于是爬蟲(chóng)應(yīng)運(yùn)而生,下面是我在開(kāi)發(fā)一個(gè)簡(jiǎn)單爬蟲(chóng)的經(jīng)過(guò)與遇到的問(wèn)題.PHP編程
??? 開(kāi)發(fā)一個(gè)爬蟲(chóng),首先你要知道你的這個(gè)爬蟲(chóng)是要用來(lái)做什么的.我是要用來(lái)去不同網(wǎng)站找特定關(guān)鍵字的文章,并獲取它的鏈接,以便我快速閱讀.PHP編程
??? 按照個(gè)人習(xí)慣,我首先要寫(xiě)一個(gè)界面,理清下思路.PHP編程
??? 1、去不同網(wǎng)站.那么我們需要一個(gè)url輸入框.
??? 2、找特定關(guān)鍵字的文章.那么我們需要一個(gè)文章標(biāo)題輸入框.
??? 3、獲取文章鏈接.那么我們需要一個(gè)搜索結(jié)果的顯示容器.PHP編程
<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">文章標(biāo)題</label> <input type="text" class="form-control" id="article_title" placeholder="文章標(biāo)題"> </div> <div class="form-group"> <label for="website_url">網(wǎng)站URL</label> <input type="text" class="form-control" id="website_url" placeholder="網(wǎng)站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"> <h3></h3> </div> </div> </div>
直接上代碼,然后加上自己的一些樣式調(diào)整,界面就完成啦:PHP編程
PHP編程
那么接下來(lái)就是功能的實(shí)現(xiàn)了,我用PHP來(lái)寫(xiě),首先第一步就是獲取網(wǎng)站的html代碼,獲取html代碼的方式也有很多,我就不一一介紹了,這里用了curl來(lái)獲取,傳入網(wǎng)站url就能得到html代碼啦:PHP編程
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代碼,但是很快你會(huì)遇到一個(gè)問(wèn)題,那就是編碼問(wèn)題,這可能讓你下一步的匹配無(wú)功而返,我們這里統(tǒng)一把得到的html內(nèi)容轉(zhuǎn)為utf8編碼:PHP編程
$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');
得到網(wǎng)站的html,要獲取文章的url,那么下一步就是要匹配該網(wǎng)頁(yè)下的所有a標(biāo)簽,需要用到正則表達(dá)式,經(jīng)過(guò)多次測(cè)試,最終得到一個(gè)比較靠譜的正則表達(dá)式,不管a標(biāo)簽下結(jié)構(gòu)多復(fù)雜,只要是a標(biāo)簽的都不放過(guò):(最關(guān)鍵的一步)PHP編程
$pattern = '|<a[^>]*>(.*)</a>|isU'; preg_match_all($pattern, $html, $matches);
匹配的結(jié)果在$matches中,它大概是這樣的一個(gè)多維素組:PHP編程
array(2) { [0]=> array(*) { [0]=> string(*) "完整的a標(biāo)簽" . . . } [1]=> array(*) { [0]=> string(*) "與上面下標(biāo)相對(duì)應(yīng)的a標(biāo)簽中的內(nèi)容" } }
只要能得到這個(gè)數(shù)據(jù),其他就完全可以操作啦,你可以遍歷這個(gè)素組,找到你想要a標(biāo)簽,然后獲取a標(biāo)簽相應(yīng)的屬性,想怎么操作就怎么操作啦,下面推薦一個(gè)類(lèi),讓你更方便操作a標(biāo)簽:PHP編程
$dom = new DOMDocument(); @$dom->loadHTML($a);//$a是上面得到的一些a標(biāo)簽 $url = new DOMXPath($dom); $hrefs = $url->evaluate('//a'); for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i); $url = $href->getAttribute('href'); //這里獲取a標(biāo)簽的href屬性 }
當(dāng)然,這只是一種方式,你也可以通過(guò)正則表達(dá)式匹配你想要的信息,把數(shù)據(jù)玩出新花樣.PHP編程
得到并匹配得出你想要的結(jié)果,下一步當(dāng)然就是傳回前端將他們顯示出來(lái)啦,把接口寫(xiě)好,然后前端用js獲取數(shù)據(jù),用jquery動(dòng)態(tài)添加內(nèi)容顯示出來(lái):PHP編程
var website_url = '你的接口地址'; $.getJSON(website_url,function(data){ if(data){ if(data.text == ''){ $('#article_url').html('<div><p>暫無(wú)該文章鏈接</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編程
PHP編程
歡迎參與《PHP實(shí)戰(zhàn):php實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的開(kāi)發(fā)》討論,分享您的想法,維易PHP學(xué)院為您提供專(zhuān)業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/7232.html