搜尋
首頁後端開發PHP問題php採集文章圖片不顯示怎麼辦

php採集文章圖片不顯示的解決方法:1、依照關鍵字收集百度搜尋結果;2、把採集到的html,依照定義的網域做一次批次轉換即可。

php採集文章圖片不顯示怎麼辦

本文操作環境:windows7系統、PHP7.1版、DELL G3電腦

php擷取文章圖片不顯示怎麼辦?

PHP CURL採集百度搜尋結果圖片不顯示問題的解決方法

1.根據關鍵字收集百度搜尋結果

根據關鍵字採集百度搜尋結果,可以使用curl實現,程式碼如下:

<p><?php <br/>function doCurl($url, $data=array(), $header=array(), $timeout=30){<br>  $ch = curl_init();<br>  curl_setopt($ch, CURLOPT_URL, $url);<br>  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);<br>  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);<br>  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br>  $response = curl_exec($ch);<br>  if($error=curl_error($ch)){<br>    die($error);<br>  }<br>  curl_close($ch);<br>  return $response;<br>}<br>$wd = '仙剑奇侠传';<br>$url = 'http://www.baidu.com/s?wd='.urlencode($wd);<br>$data = array();<br>$header = array();<br>$response = doCurl($url, $data, $header, 5);<br>echo $response;<br>?><br></p>

輸出後發現有部分圖片不能顯示

php採集文章圖片不顯示怎麼辦 

2.採集後的圖片不顯示原因分析

直接在百度中搜尋,頁面是可以顯示圖片的。使用firebug查看圖片路徑,發現採集的圖片網域與百度搜尋的圖片網域不同。

採集回傳的圖片網域t11.baidu.com

php採集文章圖片不顯示怎麼辦 

正常搜尋的圖片網域ss1.baidu.com

php採集文章圖片不顯示怎麼辦

#查看採集與正常搜尋的html,發現有個網域轉換的js是不一樣的

採集

<p>var list = {<br>    "graph.baidu.com": "http://graph.baidu.com",<br>    "t1.baidu.com":"http://t1.baidu.com",<br>    "t2.baidu.com":"http://t2.baidu.com",<br>    "t3.baidu.com":"http://t3.baidu.com",<br>    "t10.baidu.com":"http://t10.baidu.com",<br>    "t11.baidu.com":"http://t11.baidu.com",<br>    "t12.baidu.com":"http://t12.baidu.com",<br>    "i7.baidu.com":"http://i7.baidu.com",<br>    "i8.baidu.com":"http://i8.baidu.com",<br>    "i9.baidu.com":"http://i9.baidu.com",<br>};<br></p>

正常搜尋

<p>var list = {<br>    "graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K",<br>    "t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf",<br>    "t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf",<br>    "t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf",<br>    "t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq",<br>    "t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq",<br>    "t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq",<br>    "i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf",<br>    "i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf",<br>    "i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf",<br>};<br></p>

因此可以斷定是,百度根據來源位址、IP、header等參數,判斷如果是採集的,則傳回不同的js。

3.採集後圖片不顯示的解決方法

把採集到的html,依照定義的網域做一次批次轉換即可。

<p><?php <br/>function doCurl($url, $data=array(), $header=array(), $timeout=30){<br>  $ch = curl_init();<br>  curl_setopt($ch, CURLOPT_URL, $url);<br>  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);<br>  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);<br>  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br>  $response = curl_exec($ch);<br>  if($error=curl_error($ch)){<br>    die($error);<br>  }<br>  curl_close($ch);<br>  return $response;<br>}<br>// 域名转换<br>function cdomain($str){<br>  $baidu_domain = '{<br>    "http://graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K",<br>    "http://p.qiao.baidu.com":"https://sp0.baidu.com/5PoXdTebKgQFm2e88IuM_a",<br>    "http://vse.baidu.com":"https://sp3.baidu.com/6qUDsjip0QIZ8tyhnq",<br>    "http://hdpreload.baidu.com":"https://sp3.baidu.com/7LAWfjuc_wUI8t7jm9iCKT-xh_",<br>    "http://lcr.open.baidu.com":"https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv",<br>    "http://kankan.baidu.com":"https://sp3.baidu.com/7bM1dzeaKgQFm2e88IuM_a",<br>    "http://xapp.baidu.com":"https://sp2.baidu.com/yLMWfHSm2Q5IlBGlnYG",<br>    "http://dr.dh.baidu.com":"https://sp0.baidu.com/-KZ1aD0a2gU2pMbgoY3K",<br>    "http://xiaodu.baidu.com":"https://sp0.baidu.com/yLsHczq6KgQFm2e88IuM_a",<br>    "http://sensearch.baidu.com":"https://sp1.baidu.com/5b11fzupBgM18t7jm9iCKT-xh_",<br>    "http://s1.bdstatic.com":"https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K",<br>    "http://olime.baidu.com":"https://sp0.baidu.com/8bg4cTva2gU2pMbgoY3K",<br>    "http://app.baidu.com":"https://sp2.baidu.com/9_QWsjip0QIZ8tyhnq",<br>    "http://i.baidu.com":"https://sp0.baidu.com/74oIbT3kAMgDnd_",<br>    "http://c.baidu.com":"https://sp0.baidu.com/9foIbT3kAMgDnd_",<br>    "http://sclick.baidu.com":"https://sp0.baidu.com/5bU_dTmfKgQFm2e88IuM_a",<br>    "http://nsclick.baidu.com":"https://sp1.baidu.com/8qUJcD3n0sgCo2Kml5_Y_D3",<br>    "http://sestat.baidu.com":"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a",<br>    "http://eclick.baidu.com":"https://sp3.baidu.com/-0U_dTmfKgQFm2e88IuM_a",<br>    "http://api.map.baidu.com":"https://sp2.baidu.com/9_Q4sjOpB1gCo2Kml5_Y_D3",<br>    "http://ecma.bdimg.com":"https://ss1.bdstatic.com/-0U0bXSm1A5BphGlnYG",<br>    "http://ecmb.bdimg.com":"https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG",<br>    "http://t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf",<br>    "http://t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf",<br>    "http://t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf",<br>    "http://t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq",<br>    "http://t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq",<br>    "http://t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq",<br>    "http://i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf",<br>    "http://i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf",<br>    "http://i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf",<br>    "http://b1.bdstatic.com":"https://ss0.bdstatic.com/9uN1bjq8AAUYm2zgoY3K",<br>    "http://ss.bdimg.com":"https://ss1.bdstatic.com/5aV1bjqh_Q23odCf",<br>    "http://opendata.baidu.com":"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv",<br>    "http://api.open.baidu.com":"https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv",<br>    "http://tag.baidu.com":"https://sp1.baidu.com/6LMFsjip0QIZ8tyhnq",<br>    "http://f3.baidu.com":"https://sp2.baidu.com/-uV1bjeh1BF3odCf",<br>    "http://s.share.baidu.com":"https://sp0.baidu.com/5foZdDe71MgCo2Kml5_Y_D3",  <br>    "http://bdimg.share.baidu.com":"https://ss1.baidu.com/9rA4cT8aBw9FktbgoI7O1ygwehsv",<br>    "http://1.su.bdimg.com":"https://ss0.bdstatic.com/k4oZeXSm1A5BphGlnYG",<br>    "http://2.su.bdimg.com":"https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG",<br>    "http://3.su.bdimg.com":"https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG",<br>    "http://4.su.bdimg.com":"https://ss3.bdstatic.com/lPoZeXSm1A5BphGlnYG",<br>    "http://5.su.bdimg.com":"https://ss0.bdstatic.com/l4oZeXSm1A5BphGlnYG",<br>    "http://6.su.bdimg.com":"https://ss1.bdstatic.com/lvoZeXSm1A5BphGlnYG",<br>    "http://7.su.bdimg.com":"https://ss2.bdstatic.com/lfoZeXSm1A5BphGlnYG",<br>    "http://8.su.bdimg.com":"https://ss3.bdstatic.com/iPoZeXSm1A5BphGlnYG"<br>  }';<br>  $domain = json_decode($baidu_domain, true);<br>  foreach($domain as $k=>$v){<br>    $str = str_replace($k, $v, $str);<br>  }<br>  return $str;<br>}<br>$wd = '仙剑奇侠传';<br>$url = 'http://www.baidu.com/s?wd='.urlencode($wd);<br>$data = array();<br>$header = array();<br>$response = doCurl($url, $data, $header, 5);<br>echo cdomain($response); // 调用域名转换<br>?><br></p>

增加網域轉換後,所有的圖片都可以正常顯示。

php採集文章圖片不顯示怎麼辦

推薦學習:《PHP影片教學

以上是php採集文章圖片不顯示怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
酸與基本數據庫:差異和何時使用。酸與基本數據庫:差異和何時使用。Mar 26, 2025 pm 04:19 PM

本文比較了酸和基本數據庫模型,詳細介紹了它們的特徵和適當的用例。酸優先確定數據完整性和一致性,適合財務和電子商務應用程序,而基礎則側重於可用性和

PHP安全文件上傳:防止與文件相關的漏洞。PHP安全文件上傳:防止與文件相關的漏洞。Mar 26, 2025 pm 04:18 PM

本文討論了確保PHP文件上傳的確保,以防止諸如代碼注入之類的漏洞。它專注於文件類型驗證,安全存儲和錯誤處理以增強應用程序安全性。

PHP輸入驗證:最佳實踐。PHP輸入驗證:最佳實踐。Mar 26, 2025 pm 04:17 PM

文章討論了PHP輸入驗證以增強安全性的最佳實踐,重點是使用內置功能,白名單方法和服務器端驗證等技術。

PHP API率限制:實施策略。PHP API率限制:實施策略。Mar 26, 2025 pm 04:16 PM

本文討論了在PHP中實施API速率限制的策略,包括諸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之類的庫。它還涵蓋監視,動態調整速率限制和手

php密碼哈希:password_hash和password_verify。php密碼哈希:password_hash和password_verify。Mar 26, 2025 pm 04:15 PM

本文討論了使用password_hash和pyspasswify在PHP中使用密碼的好處。主要論點是,這些功能通過自動鹽,強大的哈希算法和SECH來增強密碼保護

OWASP前10 php:描述並減輕常見漏洞。OWASP前10 php:描述並減輕常見漏洞。Mar 26, 2025 pm 04:13 PM

本文討論了OWASP在PHP和緩解策略中的十大漏洞。關鍵問題包括注射,驗證損壞和XSS,並提供用於監視和保護PHP應用程序的推薦工具。

PHP XSS預防:如何預防XSS。PHP XSS預防:如何預防XSS。Mar 26, 2025 pm 04:12 PM

本文討論了防止PHP中XSS攻擊的策略,專注於輸入消毒,輸出編碼以及使用安全增強的庫和框架。

PHP接口與抽像類:何時使用。PHP接口與抽像類:何時使用。Mar 26, 2025 pm 04:11 PM

本文討論了PHP中接口和抽像類的使用,重點是何時使用。界面定義了無實施的合同,適用於無關類和多重繼承。摘要類提供常見功能

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器