PHP curl擴充實現資料抓取

來源:互聯網
上載者:User

標籤:

PHP版本:5.5.30

伺服器:apche

抓取網站地址:http://nc.mofcom.gov.cn/channel/gxdj/jghq/jg_list.shtml

抓取目標:擷取當日的價格資料

一、準備工作:

1.開啟php.ini設定檔,開啟curl功能擴充

  extension=php_curl.dll

  如果開啟之後(記得重啟apache服務),仍然不能使用curl的擴充方法,請檢查環境變數是否設定。

二、分析所要抓取的頁面,資料結構

  查看http://nc.mofcom.gov.cn/channel/gxdj/jghq/jg_list.shtml源碼。

  看到所有資料都在<tbody>和</tbody>標籤之中。

三、思路

  1.擷取當前網頁資料,截取到<tbody>和</tbody>。(其中資料為多行組成:<tr><td></td></tr>)

  2.在<tbody>和</tbody>中,逐個讀取行,和列的值即可獲得資料。

  3.當日的資料,可能在第一頁,第二頁等多頁。因此,進行翻頁讀取,直到頁面中找不到當日的資料,停止程式運行!

  總共就是三個迴圈:頁迴圈-->行迴圈-->列迴圈

四、代碼實現

//定義要抓取的頁面地址

$url=‘http://nc.mofcom.gov.cn/channel/gxdj/jghq/jg_list.shtml‘;//?page=1

//初始化curl串連
$curl=curl_init();
// 設定header
curl_setopt($curl, CURLOPT_HEADER, 0);
// 設定cURL 參數,要求結果儲存到字串中還是輸出到螢幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$flag="start";    //頁迴圈執行標誌,start:表示繼續執行。 end:標誌終止執行(本頁面找不到包含當天日期的資料)。


//開始頁迴圈
$i=1;      //$i作為page的值,控制翻頁
$myResultSet=[];//定義數組,用於存放所有行的資料
while ($i>0)//迴圈停止條件為 :本頁面找不到包含當天日期的資料
{
    if($flag=="end"){
        //停止頁迴圈
        break;
    }
    //設定所要抓取的網頁地址,及頁數
    curl_setopt($curl, CURLOPT_URL,$url."?page=".$i);    
    
    // 運行cURL,請求網頁
    $data = curl_exec($curl);
    
    //通過substr和strpos函數,擷取<tbody>和</tbody>之間的資料
    $mycontent=substr($data, strpos($data, "<tbody>"),strpos($data, "</tbody>")-strpos($data, "<tbody>"));        
    
    //<table>行迴圈讀取(<tr>)
    while(strpos($mycontent, "<tr>")!==false){//如果行存在strpos($mycontent, "<tr>")  
         $myResultTr=[];                      //定義局部行數組。用於存放一行的資料,每次行迴圈重新定義,以清空數組中上一行的資料
         //擷取行
         $mytr=substr($mycontent, strpos($mycontent, "<tr>"),strpos($mycontent, "</tr>")-strpos($mycontent, "<tr>"));
         //如果不是當前日期的資料、設定程式終止標誌、退出迴圈
         if(strpos($mytr, date("Y-m-d"))===false){             //本頁面找不到包含當天日期的資料,
             $flag="end";                                                    //設定標誌為end。
             break;
         }
         //列迴圈 擷取前四列
         $j=0;      //列標識
         //&&strpos($mytr, date("Y-m-d"))!==false
         while(strpos($mytr, "<td>")!==false){
             if($j>=4){   //第4列後的資料是不需要的。
                 break;
             }
             //擷取列
             $mytd=substr($mytr, strpos($mytr, "<td>"),strpos($mytr, "</td>")-strpos($mytr, "<td>"));
              
             $pre=array(" "," ","\t","\n","\r");
             //str_replace將值中包含的空格、定位字元、分行符號、斷行符號去除。  strip_tags去除html標籤
             //echo str_replace($pre, ‘‘, strip_tags($mytd));
             
             //將列資料,壓入行結果中
             array_push($myResultTr, str_replace($pre, ‘‘, strip_tags($mytd)));
              
             $mytr=substr($mytr, strpos($mytr, "</td>")+5);
             $j++;            
         }
         
         //將行結果,壓入到總結果集中
         array_push($myResultSet, $myResultTr);
         
         //改變$mycontent (截取掉第一行資料,保留剩下資料
         $mycontent=substr($mycontent, strpos($mycontent, "</tr>")+5);
     }               
    
    $i++;
}
var_dump($myResultSet);   //列印出抓取到的所有資料
echo "application stop in page".--$i;
// 關閉URL請求
curl_close($curl);

五、測試結果:

  抓取到127條資料, 網頁執行到第10頁的時候停止下來。

  

   

 

PHP curl擴充實現資料抓取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.