PHP使用curl製作簡易百度搜尋

來源:互聯網
上載者:User
這幾天研究了一下php中的curl類庫,做了一個簡單的百度搜尋,先上代碼

<div style="width:200px;height:100px;"> <div>百度搜尋</div> <form action="" method="get">  <input type="text" name="key">  <input type="submit" value="搜尋"> </form></div><?php$k = '';$k = !empty($_GET['key'])?$_GET['key']:'';session_start();$_SESSION['key'] = $k; $curl = curl_init(); // 設定你需要抓取的URL  for($i = 0;$i<2;$i++){curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com/s?wd={$_SESSION['key']}&pn={$i}"); // 設定header curl_setopt($curl, CURLOPT_HEADER, 1); // 設定cURL 參數,要求結果儲存到字串中還是輸出到螢幕上。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 運行cURL,請求網頁 $data = curl_exec($curl);  $pre = '/<h3 class="t"><a.*?href = "(.*?)".*?target="_blank".*?>(.*?)<\/a><\/h3>/s';preg_match_all($pre,$data,$match); foreach ($match[1] as $k => $v) {?> <div style="font-size:20px;color:red;"> <a href="<?php echo $v;?>" target="_blank"><?php echo strip_tags($match[2][$k]);?></a></div><?php}} curl_close($curl);  ?>

經過分析百度的搜尋時的url發現有一個規律

https://www.baidu.com/s?wd=搜尋的關鍵字

但是我發現使用https協議後不能夠獲得百度上的資料於是改為http://www.baidu.com?wd=搜尋的關鍵字就可以啦!!

如下:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多topic.alibabacloud.com。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.