PHP一個簡單的無需刷新爬蟲
更新時間:2019年01月05日 14:50:01 作者:只是個寶寶
今天小編就為大家分享一篇關于PHP一個簡單的無需刷新爬蟲,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
由于只是一個小示例,所以過程化簡單寫了,小菜隨便參考,大神大可點解
<?php
//設置最大執(zhí)行時間
set_time_limit(0);
function getHtml($url){
// 1. 初始化
$ch = curl_init();
// 2. 設置選項,包括URL
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
// 3. 執(zhí)行并獲取HTML文檔內容
$output = curl_exec($ch);
if($output === FALSE ){
$output = '';
}
// 4. 釋放curl句柄
curl_close($ch);
return $output;
}
function getPageData($url){
// 獲取整個網頁內容
$html = getHtml($url);
// 初步獲取主塊內容
preg_match("/教程列表.*教程列表/s",$html,$body_html);
// 返回數據
$data = array();
//判斷是否存在要獲取的內容
if(count($body_html)){
// 獲取頁面指定信息
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存數組,以及覆蓋舊數據
$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>';
// 初始化數據
$page_no = 1;
$data_all = array();
// 分頁獲取數據
do{
$url = 'http://www.thinkphp.cn/code/examples/p/' . $page_no;
$data = getPageData($url);
$data_all += $data;
$page_no ++;
}while ($page_no <= 10); //當前只獲取10頁,如果要全部獲取則把條件換成$data或!empty($data)
var_dump($data_all);
?>
接下的入表庫當然就不寫了,那些更小意思了~就此別過吧~
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
Google Voice 短信發(fā)送接口PHP開源版(2010.5更新)
Google Voice 短信發(fā)送接口PHP開源版,以前的版本不能用了,作者于2010年5月進行了更新。2010-07-07
smarty模板引擎中內建函數if、elseif和else的使用方法
這篇文章主要介紹了smarty模板引擎中內建函數if、elseif和else的使用方法,通過兩個示例分析了if、elseif和else的使用技巧,需要的朋友可以參考下2015-01-01

