創(chuàng)建一個新cURL資源
10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有朗縣免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
設(shè)置URL和相應(yīng)的選項
抓取URL并把它傳遞給瀏覽器
關(guān)閉cURL資源,并且釋放系統(tǒng)資源
代碼案例:
pcntl_fork或者swoole_process實現(xiàn)多進(jìn)程并發(fā)。按照每個網(wǎng)頁抓取耗時500ms,開200個進(jìn)程,可以實現(xiàn)每秒400個頁面的抓取。
curl實現(xiàn)頁面抓取,設(shè)置cookie可以實現(xiàn)模擬登錄
simple_html_dom 實現(xiàn)頁面的解析和DOM處理
如果想要模擬瀏覽器,可以使用casperJS。用swoole擴(kuò)展封裝一個服務(wù)接口給PHP層調(diào)用
在這里有一套爬蟲系統(tǒng)就是基于上述技術(shù)方案實現(xiàn)的,每天會抓取幾千萬個頁面。
本文承接上面兩篇,本篇中的示例要調(diào)用到前兩篇中的函數(shù),做一個簡單的URL采集。一般php采集網(wǎng)絡(luò)數(shù)據(jù)會用file_get_contents、file和cURL。不過據(jù)說cURL會比file_get_contents、file更快更專業(yè),更適合采集。今天就試試用cURL來獲取網(wǎng)頁上的所有鏈接。示例如下:
?php
/*
* 使用curl 采集hao123.com下的所有鏈接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 頁面內(nèi)容我們并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回結(jié)果,而不是輸出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主機(jī)部分,補(bǔ)全用
$host = '';
if (is_array($linkarr)) {
foreach ($linkarr as $k = $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("p此頁面的所有鏈接為:/ppre%s/pren", var_export($linkresult , true));
?
function.php內(nèi)容如下(即為上兩篇中兩個函數(shù)的合集):
?php
function _striplinks($document) {
preg_match_all("'s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?
從爬蟲基本要求來看:
抓?。鹤ト∽罨揪褪抢W(wǎng)頁回來,所以第一步就是拉網(wǎng)頁回來,慢慢會發(fā)現(xiàn)各種問題待優(yōu)化;
存儲:抓回來一般會用一定策略存下來,可以選擇存文件系統(tǒng)開始,然后以一定規(guī)則命名。
分析:對網(wǎng)頁進(jìn)行文本分析,可以用認(rèn)為最快最優(yōu)的辦法,比如正則表達(dá)式;
展示:要是做了一堆事情,一點展示輸出都沒有,如何展現(xiàn)價值。
header("Content-Type: text/html; charset=gbk");
$url = "";
$fcontents = file_get_contents($url);
if (ereg("title(.*)/title", $fcontents, $regs)){echo "ok";}else{echo "error";}
echo "br";
print_r($regs);
其實用PHP來爬會非常方便,主要是PHP的正則表達(dá)式功能在搜集頁面連接方面很方便,另外PHP的fopen、file_get_contents以及l(fā)ibcur的函數(shù)非常方便的下載網(wǎng)頁內(nèi)容。
具體處理方式就是建立就一個任務(wù)隊列,往隊列里面插入一些種子任務(wù)和可以開始爬行,爬行的過程就是循環(huán)的從隊列里面提取一個URL,打開后獲取連接插入隊列中,進(jìn)行相關(guān)的保存。隊列可以使用數(shù)組實現(xiàn)。
當(dāng)然PHP作為但線程的東西,慢慢爬還是可以,怕的就是有的URL打不開,會死在那里。
網(wǎng)站欄目:php爬蟲獲取網(wǎng)頁數(shù)據(jù)庫,python爬蟲獲取網(wǎng)頁數(shù)據(jù)
標(biāo)題來源:http://sd-ha.com/article26/dsihccg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、響應(yīng)式網(wǎng)站、軟件開發(fā)、微信公眾號、網(wǎng)站設(shè)計、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)