PHP CURL采集百度搜尋結(jié)果圖片不顯示問(wèn)題的解決方法
1.根據(jù)關(guān)鍵字采集百度搜尋結(jié)果
根據(jù)關(guān)鍵字采集百度搜尋結(jié)果,可以使用curl實(shí)現(xiàn),代碼如下:
<?php function doCurl($url, $data=array(), $header=array(), $timeout=30){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); return $response; } $wd = '仙劍奇?zhèn)b傳'; $url = 'http://www.baidu.com/s?wd='.urlencode($wd); $data = array(); $header = array(); $response = doCurl($url, $data, $header, 5); echo $response; ?>
輸出后發(fā)現(xiàn)有部分圖片不能顯示
2.采集后的圖片不顯示原因分析
直接在百度中搜尋,頁(yè)面是可以顯示圖片的。使用firebug查看圖片路徑,發(fā)現(xiàn)采集的圖片域名與在百度搜尋的圖片域名不同。
采集返回的圖片域名 t11.baidu.com
正常搜尋的圖片域名 ss1.baidu.com
查看采集與正常搜尋的html,發(fā)現(xiàn)有個(gè)域名轉(zhuǎn)換的js是不一樣的
采集
var list = { "graph.baidu.com": "http://graph.baidu.com", "t1.baidu.com":"http://t1.baidu.com", "t2.baidu.com":"http://t2.baidu.com", "t3.baidu.com":"http://t3.baidu.com", "t10.baidu.com":"http://t10.baidu.com", "t11.baidu.com":"http://t11.baidu.com", "t12.baidu.com":"http://t12.baidu.com", "i7.baidu.com":"http://i7.baidu.com", "i8.baidu.com":"http://i8.baidu.com", "i9.baidu.com":"http://i9.baidu.com", };
正常搜尋
var list = { "graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K", "t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf", "t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf", "t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf", "t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq", "t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq", "t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq", "i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf", "i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf", "i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf", };
因此可以斷定是,百度根據(jù)來(lái)源地址、IP、header等參數(shù),判斷如果是采集的,則返回不同的js。
3.采集后圖片不顯示的解決方法
把采集到的html,根據(jù)定義的域名做一次批量轉(zhuǎn)換即可。
<?php function doCurl($url, $data=array(), $header=array(), $timeout=30){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); return $response; } // 域名轉(zhuǎn)換 function cdomain($str){ $baidu_domain = '{ "http://graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K", "http://p.qiao.baidu.com":"https://sp0.baidu.com/5PoXdTebKgQFm2e88IuM_a", "http://vse.baidu.com":"https://sp3.baidu.com/6qUDsjip0QIZ8tyhnq", "http://hdpreload.baidu.com":"https://sp3.baidu.com/7LAWfjuc_wUI8t7jm9iCKT-xh_", "http://lcr.open.baidu.com":"https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv", "http://kankan.baidu.com":"https://sp3.baidu.com/7bM1dzeaKgQFm2e88IuM_a", "http://xapp.baidu.com":"https://sp2.baidu.com/yLMWfHSm2Q5IlBGlnYG", "http://dr.dh.baidu.com":"https://sp0.baidu.com/-KZ1aD0a2gU2pMbgoY3K", "http://xiaodu.baidu.com":"https://sp0.baidu.com/yLsHczq6KgQFm2e88IuM_a", "http://sensearch.baidu.com":"https://sp1.baidu.com/5b11fzupBgM18t7jm9iCKT-xh_", "http://s1.bdstatic.com":"https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K", "http://olime.baidu.com":"https://sp0.baidu.com/8bg4cTva2gU2pMbgoY3K", "http://app.baidu.com":"https://sp2.baidu.com/9_QWsjip0QIZ8tyhnq", "http://i.baidu.com":"https://sp0.baidu.com/74oIbT3kAMgDnd_", "http://c.baidu.com":"https://sp0.baidu.com/9foIbT3kAMgDnd_", "http://sclick.baidu.com":"https://sp0.baidu.com/5bU_dTmfKgQFm2e88IuM_a", "http://nsclick.baidu.com":"https://sp1.baidu.com/8qUJcD3n0sgCo2Kml5_Y_D3", "http://sestat.baidu.com":"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a", "http://eclick.baidu.com":"https://sp3.baidu.com/-0U_dTmfKgQFm2e88IuM_a", "http://api.map.baidu.com":"https://sp2.baidu.com/9_Q4sjOpB1gCo2Kml5_Y_D3", "http://ecma.bdimg.com":"https://ss1.bdstatic.com/-0U0bXSm1A5BphGlnYG", "http://ecmb.bdimg.com":"https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG", "http://t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf", "http://t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf", "http://t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf", "http://t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq", "http://t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq", "http://t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq", "http://i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf", "http://i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf", "http://i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf", "http://b1.bdstatic.com":"https://ss0.bdstatic.com/9uN1bjq8AAUYm2zgoY3K", "http://ss.bdimg.com":"https://ss1.bdstatic.com/5aV1bjqh_Q23odCf", "http://opendata.baidu.com":"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv", "http://api.open.baidu.com":"https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv", "http://tag.baidu.com":"https://sp1.baidu.com/6LMFsjip0QIZ8tyhnq", "http://f3.baidu.com":"https://sp2.baidu.com/-uV1bjeh1BF3odCf", "http://s.share.baidu.com":"https://sp0.baidu.com/5foZdDe71MgCo2Kml5_Y_D3", "http://bdimg.share.baidu.com":"https://ss1.baidu.com/9rA4cT8aBw9FktbgoI7O1ygwehsv", "http://1.su.bdimg.com":"https://ss0.bdstatic.com/k4oZeXSm1A5BphGlnYG", "http://2.su.bdimg.com":"https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG", "http://3.su.bdimg.com":"https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG", "http://4.su.bdimg.com":"https://ss3.bdstatic.com/lPoZeXSm1A5BphGlnYG", "http://5.su.bdimg.com":"https://ss0.bdstatic.com/l4oZeXSm1A5BphGlnYG", "http://6.su.bdimg.com":"https://ss1.bdstatic.com/lvoZeXSm1A5BphGlnYG", "http://7.su.bdimg.com":"https://ss2.bdstatic.com/lfoZeXSm1A5BphGlnYG", "http://8.su.bdimg.com":"https://ss3.bdstatic.com/iPoZeXSm1A5BphGlnYG" }'; $domain = json_decode($baidu_domain, true); foreach($domain as $k=>$v){ $str = str_replace($k, $v, $str); } return $str; } $wd = '仙劍奇?zhèn)b傳'; $url = 'http://www.baidu.com/s?wd='.urlencode($wd); $data = array(); $header = array(); $response = doCurl($url, $data, $header, 5); echo cdomain($response); // 調(diào)用域名轉(zhuǎn)換 ?>
增加域名轉(zhuǎn)換后,所有的圖片都可以正常顯示。
以上所述是小編給大家介紹的PHP CURL采集百度搜尋結(jié)果圖片不顯示問(wèn)題的解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- PHP多線程批量采集下載美女圖片的實(shí)現(xiàn)代碼(續(xù))
- PHP批量采集下載圖片的實(shí)現(xiàn)代碼
- php curl簡(jiǎn)單采集圖片生成base64編碼(并附curl函數(shù)參數(shù)說(shuō)明)
- php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法
- PHP遠(yuǎn)程采集圖片詳細(xì)教程
- PHP采集類(lèi)Snoopy抓取圖片實(shí)例
- phpphp圖片采集后按原路徑保存圖片示例
- php采集文章中的圖片獲取替換到本地(實(shí)現(xiàn)代碼)
- PHP 文章中的遠(yuǎn)程圖片采集到本地的代碼
- PHP下載采集圖片到本地的方法詳解【可忽略ssl認(rèn)證】
相關(guān)文章
Yii2.0框架模型添加/修改/刪除數(shù)據(jù)操作示例
這篇文章主要介紹了Yii2.0框架模型添加/修改/刪除數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了Yii2.0使用模型操作數(shù)據(jù)的添加、修改、刪除相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07OAuth認(rèn)證協(xié)議中的HMACSHA1加密算法(實(shí)例)
下面小編就為大家?guī)?lái)一篇OAuth認(rèn)證協(xié)議中的HMACSHA1加密算法(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10如何把php5.3版本升級(jí)到php5.4或者php5.5
這篇文章主要介紹了如何把php5.3升級(jí)到php5.4或者php5.5,需要的朋友可以參考下2015-07-07PHP編譯安裝時(shí)常見(jiàn)錯(cuò)誤解決辦法
這篇文章主要介紹了PHP編譯安裝時(shí)常見(jiàn)錯(cuò)誤解決辦法,本文涵蓋了PHP編譯安裝中的大多數(shù)錯(cuò)誤,同時(shí)給出解決方法,需要的朋友可以參考下2015-05-05Drupal7中常用的數(shù)據(jù)庫(kù)操作實(shí)例
Drupal 7 提供的新功能其中一個(gè)就是可以使用 Query Builder and Query Objects 查詢生成器來(lái)構(gòu)造查詢對(duì)象的能力,無(wú)需在代碼中寫(xiě)原始的SQL語(yǔ)句,一是提高了代碼可閱讀性,二是兼容所有數(shù)據(jù)庫(kù)2014-03-03Centos下升級(jí)php5.2到php5.4全記錄(編譯安裝)
這篇文章主要介紹了Centos下升級(jí)php5.2到php5.4全記錄(編譯安裝),本文還同時(shí)講解了解決htmlspecialchars默認(rèn)編碼UTF-8問(wèn)題的解決方法,需要的朋友可以參考下2015-04-04php中使用session_set_save_handler()函數(shù)把session保存到MySQL數(shù)據(jù)庫(kù)實(shí)例
這篇文章主要介紹了php中使用session_set_save_handler()函數(shù)把session保存到MySQL數(shù)據(jù)庫(kù)實(shí)例,本文同時(shí)還給出了Session保存到Mysql數(shù)據(jù)庫(kù)存儲(chǔ)類(lèi),需要的朋友可以參考下2014-11-11