php網(wǎng)絡(luò)數(shù)據(jù)爬取 php抓取網(wǎng)頁數(shù)據(jù)

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

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

10年積累的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有潁泉免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

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

本文承接上面兩篇,本篇中的示例要調(diào)用到前兩篇中的函數(shù),做一個(gè)簡(jiǎn)單的URL采集。一般php采集網(wǎng)絡(luò)數(shù)據(jù)會(huì)用file_get_contents、file和cURL。不過據(jù)說cURL會(huì)比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)容如下(即為上兩篇中兩個(gè)函數(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采集網(wǎng)站數(shù)據(jù)

簡(jiǎn)單的分了幾個(gè)步驟:

1、確定采集目標(biāo)

2、獲取目標(biāo)遠(yuǎn)程頁面內(nèi)容(curl、file_get_contents)

3、分析頁面html源碼,正則匹配你需要的內(nèi)容(preg_match、preg_match_all),這一步最為重要,不同頁面正則匹配規(guī)則不一樣

4、入庫

php的curl怎么爬取網(wǎng)頁內(nèi)容

創(chuàng)建一個(gè)新cURL資源

設(shè)置URL和相應(yīng)的選項(xiàng)

抓取URL并把它傳遞給瀏覽器

關(guān)閉cURL資源,并且釋放系統(tǒng)資源

代碼案例:

網(wǎng)頁標(biāo)題:php網(wǎng)絡(luò)數(shù)據(jù)爬取 php抓取網(wǎng)頁數(shù)據(jù)
網(wǎng)頁URL:http://muchs.cn/article14/doeddde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、微信公眾號(hào)、標(biāo)簽優(yōu)化外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)