閱讀420 返回首頁    go 中電雲集


php數據抓取

我們得到輸入關鍵字後在百度上搜索出來的第一條記錄。例如:

333

代碼如下:

<?php
if($_POST['donow']){
    if($_POST['keys']){
         $keys = trim($_POST['keys']);
         $keystr = backstr($keys);
         $urls = “https://www.baidu.com/s?wd=$keystr“;
         $content = file_get_contents($urls);
         $c1 = “/<table (border=”0″)?(s)?cellpadding=”0″ cellspacing=”0″ class=”result” id=”(.*)”>(.*)</table><br>/Uis”;
          preg_match_all($c1,$content,$arr1);
          echo $arr1[0][0];
    }
}

//處理字符串函數,將字符串處理成百度搜索格式的
function backstr($keys){
     $keysa  = explode(” “,$keys);
     $output = ”;
     $ara    = array();
     for($k=0;$k<sizeof($keysa);$k++){
         if(!preg_match(’/(w).*/’,$keysa[$k],$arrk)){
             $output   = ”;
             $tab_text = str_split($keysa[$k]);
              foreach ($tab_text as $id=>$char){
                    $hex    = dechex(ord($char));
                    $output.= ‘%’ . $hex;
              }
          }else{
               $output = $keysa[$k];
          }
          array_push($ara,$output);
      }
      $output = implode(”+”,$ara);
      return $output;
}
?>
<form action=”" method=”POST”>
 <table width=”30%” border=”1″ align=”center”>
  <tr><td>關鍵字:<input type=”text” name=”keys” id=”keys” value=”<?php echo $keys;?>” size=”30″></td></tr>
  <tr><td><input type=”submit” name=”donow” value=”立即搜索”></tr>
 </table>
</form>

得到結果:

121

注:本php函數需要您的配置環境支持的前提才能抓取到數據;

         抓取不同的頁麵需要修改相應的正則表達式。

最後更新:2017-01-04 22:34:39

  上一篇:go 一、phpwind數據轉移
  下一篇:go 如何創建一套完整獨立的Discuz X2模版