由于只是一個小示例,所以過程化簡單寫了,小菜隨便參考,大神大可點解
<?php //設置最大執行時間 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. 執行并獲取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); ?>
接下的入表庫當然就不寫了,那些更小意思了~就此別過吧~
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。