PHP CURL采集百度搜寻结果图片不显示问题的解决方

网络编程 2021-07-05 08:23www.168986.cn编程入门
这篇文章主要介绍了PHP CURL采集百度搜寻结果图片不显示问题的解决方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

1.根据关键字采集百度搜寻结果

根据关键字采集百度搜寻结果,可以使用curl实现,代码如下

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

输出后发现有部分图片不能显示

 

2.采集后的图片不显示原因分析

直接在百度中搜寻,页面是可以显示图片的。使用firebug查看图片路径,发现采集的图片域名与在百度搜寻的图片域名不同。

采集返回的图片域名 t11.baidu.

 

正常搜寻的图片域名 ss1.baidu.

 

查看采集与正常搜寻的html,发现有个域名转换的js是不一样的

采集

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

正常搜寻

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

可以断定是,百度根据来源地址、IP、header等参数,判断如果是采集的,则返回不同的js。

3.采集后图片不显示的解决方法

把采集到的html,根据定义的域名做一次批量转换即可。

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

增加域名转换后,所有的图片都可以正常显示。

以上所述是长沙网络推广给大家介绍的PHP CURL采集百度搜寻结果图片不显示问题的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by