php采集文章图片不显示的解决办法:1、根据关键字采集百度搜寻结果;2、把采集到的html,根据定义的域名做一次批量转换即可。
本文操作环境: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>
输出后发现有部分图片不能显示
2.采集后的图片不显示原因分析
直接在百度中搜寻,页面是可以显示图片的。使用firebug查看图片路径,发现采集的图片域名与在百度搜寻的图片域名不同。
采集返回的图片域名 t11.baidu.com
正常搜寻的图片域名 ss1.baidu.com
查看采集与正常搜寻的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文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),