欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP使用curl制作簡(jiǎn)易百度搜索

 更新時(shí)間:2016年11月03日 08:51:13   作者:六月天空的芬芳  
這篇文章主要為大家詳細(xì)介紹了PHP使用curl制作簡(jiǎn)易百度搜索的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

這幾天研究了一下php中的curl類庫(kù),做了一個(gè)簡(jiǎn)單的百度搜索,先上代碼

<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(); 
// 設(shè)置你需要抓取的URL 

for($i = 0;$i<2;$i++){
curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com/s?wd={$_SESSION['key']}&pn={$i}"); 
// 設(shè)置header 
curl_setopt($curl, CURLOPT_HEADER, 1); 
// 設(shè)置cURL 參數(shù),要求結(jié)果保存到字符串中還是輸出到屏幕上。 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
// 運(yùn)行cURL,請(qǐng)求網(wǎng)頁(yè) 
$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);


?>

經(jīng)過(guò)分析百度的搜索時(shí)的url發(fā)現(xiàn)有一個(gè)規(guī)律

https://www.baidu.com/s?wd=搜索的關(guān)鍵字

但是我發(fā)現(xiàn)使用https協(xié)議后不能夠獲得百度上的數(shù)據(jù)于是改為http://www.baidu.com?wd=搜索的關(guān)鍵字就可以啦!!

效果圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論