欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php如何獲取爬蟲數(shù)據(jù) php爬蟲數(shù)據(jù)采集

如何用PHP做網(wǎng)絡爬蟲

其實用PHP來爬會非常方便,主要是PHP的正則表達式功能在搜集頁面連接方面很方便,另外PHP的fopen、file_get_contents以及l(fā)ibcur的函數(shù)非常方便的下載網(wǎng)頁內(nèi)容。

成都創(chuàng)新互聯(lián)公司企業(yè)建站,十多年網(wǎng)站建設經(jīng)驗,專注于網(wǎng)站建設技術(shù),精于網(wǎng)頁設計,有多年建站和網(wǎng)站代運營經(jīng)驗,設計師為客戶打造網(wǎng)絡企業(yè)風格,提供周到的建站售前咨詢和貼心的售后服務。對于成都網(wǎng)站設計、成都做網(wǎng)站中不同領域進行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設中充分了解客戶行業(yè)的需求,以靈動的思維在網(wǎng)頁中充分展現(xiàn),通過對客戶行業(yè)精準市場調(diào)研,為客戶提供的解決方案。

具體處理方式就是建立就一個任務隊列,往隊列里面插入一些種子任務和可以開始爬行,爬行的過程就是循環(huán)的從隊列里面提取一個URL,打開后獲取連接插入隊列中,進行相關(guān)的保存。隊列可以使用數(shù)組實現(xiàn)。

當然PHP作為但線程的東西,慢慢爬還是可以,怕的就是有的URL打不開,會死在那里。

php如何爬取天貓和淘寶商品數(shù)據(jù)

直接用Curl就行,具體爬取的數(shù)據(jù)可以穿參查看結(jié)果,方法不區(qū)分淘寶和天貓鏈接,但是前提是必須是PC端鏈接,另外正則寫的不規(guī)范,所以可以自己重寫正則來匹配數(shù)據(jù)。

php中curl爬蟲 怎么樣通過網(wǎng)頁獲取所有鏈接

本文承接上面兩篇,本篇中的示例要調(diào)用到前兩篇中的函數(shù),做一個簡單的URL采集。一般php采集網(wǎng)絡數(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);

// 主機部分,補全用

$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;

}

?

當前名稱:php如何獲取爬蟲數(shù)據(jù) php爬蟲數(shù)據(jù)采集
文章起源:http://chinadenli.net/article6/doopgig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航品牌網(wǎng)站建設網(wǎng)站建設響應式網(wǎng)站做網(wǎng)站

廣告

聲明:本網(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)

小程序開發(fā)