PHP一個簡單的無需刷新爬蟲
更新時間:2019年01月05日 14:50:01 作者:只是個寶寶
今天小編就為大家分享一篇關(guān)于PHP一個簡單的無需刷新爬蟲,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
由于只是一個小示例,所以過程化簡單寫了,小菜隨便參考,大神大可點(diǎn)解
<?php //設(shè)置最大執(zhí)行時間 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){ // 獲取整個網(wǎng)頁內(nèi)容 $html = getHtml($url); // 初步獲取主塊內(nèi)容 preg_match("/教程列表.*教程列表/s",$html,$body_html); // 返回數(shù)據(jù) $data = array(); //判斷是否存在要獲取的內(nèi)容 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存數(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(); // 分頁獲取數(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頁,如果要全部獲取則把條件換成$data或!empty($data) var_dump($data_all); ?>
接下的入表庫當(dāng)然就不寫了,那些更小意思了~就此別過吧~
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Google Voice 短信發(fā)送接口PHP開源版(2010.5更新)
Google Voice 短信發(fā)送接口PHP開源版,以前的版本不能用了,作者于2010年5月進(jìn)行了更新。2010-07-07smarty模板引擎中內(nèi)建函數(shù)if、elseif和else的使用方法
這篇文章主要介紹了smarty模板引擎中內(nèi)建函數(shù)if、elseif和else的使用方法,通過兩個示例分析了if、elseif和else的使用技巧,需要的朋友可以參考下2015-01-01PHP圖像識別技術(shù)原理與實(shí)現(xiàn)
本篇文章主要介紹了PHP圖像識別技術(shù)原理與實(shí)現(xiàn),這個平時做的密碼驗(yàn)證有異曲同工之處,有需要的可以了解一下。2016-10-10