欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂PHP培訓之實現(xiàn)一個簡單的無需刷新爬蟲

2019-01-07 10:40:27 1393瀏覽

今天扣丁學堂PHP培訓老師給大家分享一篇關于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文檔內(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://m.dionly.net.cn/article?page=2' . $page_no;
  $data = getPageData($url);
  $data_all += $data;
  $page_no ++;
}while ($page_no <= 10); //當前只獲取10頁,如果要全部獲取則把條件換成$data或!empty($data)
var_dump($data_all);
?>

以上就是關于扣丁學堂PHP培訓之實現(xiàn)一個簡單的無需刷新爬蟲的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,想要了解更多關于PHP開發(fā)方面內(nèi)容的小伙伴,請關注扣丁學堂PHP培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育平臺為您提供權(quán)威的PHP開發(fā)視頻教程,除此之外扣丁學堂還有PHP從入門到精通的學習路線圖供大家參考哦??鄱W堂PHP技術交流群:374332265。



【關注微信公眾號獲取更多學習資料】

 

查看更多關于“php培訓資訊”的相關文章>>

標簽: PHP培訓 PHP視頻教程 PHP在線視頻 PHP學習視頻

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部