PHP一個(gè)簡(jiǎn)單的無(wú)需刷新爬蟲
由于只是一個(gè)小示例,所以過(guò)程化簡(jiǎn)單寫了,小菜隨便參考,大神大可點(diǎn)解
<?php
//設(shè)置最大執(zhí)行時(shí)間
set_time_limit(0);
function getHtml($url){
// 1. 初始化
$ch = curl_init();
// 2. 設(shè)置選項(xiàng),包括URL
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
// 3. 執(zhí)行并獲取HTML文檔內(nèi)容
$output = curl_exec($ch);
if($output === FALSE ){
$output = '';
}
// 4. 釋放curl句柄
curl_close($ch);
return $output;
}
function getPageData($url){
// 獲取整個(gè)網(wǎng)頁(yè)內(nèi)容
$html = getHtml($url);
// 初步獲取主塊內(nèi)容
preg_match("/教程列表.*教程列表/s",$html,$body_html);
// 返回?cái)?shù)據(jù)
$data = array();
//判斷是否存在要獲取的內(nèi)容
if(count($body_html)){
// 獲取頁(yè)面指定信息
preg_match_all('/<a class="avatar".*user_id="(\S*)" href="(\S*)" rel="external nofollow" /',$body_html[0],$info_1);
preg_match_all('/<a href="(.*)" rel="external nofollow" .*title="(.*)"/',$body_html[0],$info_2);
$info = array_merge($info_1,$info_2);
//組合的信息
for($index=0; $index<count($info[0]); $index++){
//以文章信息作為key存數(shù)組,以及覆蓋舊數(shù)據(jù)
$data[$info[4][$index]] = array(
'user_id' => $info[1][$index],
'user_home' => $info[2][$index],
'a_url' => $info[4][$index],
'a_title' => $info[5][$index],
);
}
}
return $data;
}
header("Content-type: text/html; charset=utf-8");
echo '<pre>';
// 初始化數(shù)據(jù)
$page_no = 1;
$data_all = array();
// 分頁(yè)獲取數(shù)據(jù)
do{
$url = 'http://www.thinkphp.cn/code/examples/p/' . $page_no;
$data = getPageData($url);
$data_all += $data;
$page_no ++;
}while ($page_no <= 10); //當(dāng)前只獲取10頁(yè),如果要全部獲取則把條件換成$data或!empty($data)
var_dump($data_all);
?>
接下的入表庫(kù)當(dāng)然就不寫了,那些更小意思了~就此別過(guò)吧~
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
php使用phpoffice/phpspreadsheet導(dǎo)出圖片實(shí)例
這篇文章主要為大家介紹了php使用phpoffice/phpspreadsheet導(dǎo)出圖片實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
CI框架實(shí)現(xiàn)cookie登陸的方法詳解
這篇文章主要介紹了CI框架實(shí)現(xiàn)cookie登陸的方法,結(jié)合實(shí)例形式分析了CI框架使用cookie實(shí)現(xiàn)登陸的步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-05-05
PHP+百度AI OCR文字識(shí)別實(shí)現(xiàn)了圖片的文字識(shí)別功能
這篇文章主要介紹了PHP+百度AI OCR文字識(shí)別實(shí)現(xiàn)了圖片的文字識(shí)別功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
Bootstrap+PHP實(shí)現(xiàn)多圖上傳功能實(shí)例詳解
這篇文章主要介紹了Bootstrap+PHP實(shí)現(xiàn)多圖上傳功能實(shí)例詳解,本文圖片加實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04

