php爬取頁(yè)面數(shù)據(jù) php實(shí)現(xiàn)爬蟲

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

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

在網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營(yíng)銷成為有效果、有回報(bào)的無(wú)錫營(yíng)銷推廣。創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站建設(shè)十余年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。

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

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

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

代碼案例:

php如何爬取微博熱門視屏

1.訪問(wèn) genvisitor 獲取一個(gè)tid.

2.用tid 先獲取一個(gè) cookie,同時(shí)得到2個(gè)參數(shù) sub subp。

3.帶著獲得的cookie,和sub subp 2個(gè)參數(shù) 獲得第二個(gè)cookie。

4.最后用第二個(gè)cookie訪問(wèn)要抓取的頁(yè)面。

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

本文承接上面兩篇,本篇中的示例要調(diào)用到前兩篇中的函數(shù),做一個(gè)簡(jiǎn)單的URL采集。一般php采集網(wǎng)絡(luò)數(shù)據(jù)會(huì)用file_get_contents、file和cURL。不過(guò)據(jù)說(shuō)cURL會(huì)比f(wàn)ile_get_contents、file更快更專業(yè),更適合采集。今天就試試用cURL來(lái)獲取網(wǎng)頁(yè)上的所有鏈接。示例如下:

?php

/*

* 使用curl 采集hao123.com下的所有鏈接。

*/

include_once('function.php');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '');

// 只需返回HTTP header

curl_setopt($ch, CURLOPT_HEADER, 1);

// 頁(yè)面內(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此頁(yè)面的所有鏈接為:/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;

}

?

文章標(biāo)題:php爬取頁(yè)面數(shù)據(jù) php實(shí)現(xiàn)爬蟲
新聞來(lái)源:http://www.muchs.cn/article24/doeejce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站導(dǎo)航、手機(jī)網(wǎng)站建設(shè)電子商務(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名