1.根据关键字采集百度搜寻结果
根据关键字采集百度搜寻结果,可以使用curl实现,代码如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?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 = '仙剑奇侠传' ; $url = ' http://www.baidu.com/s?wd= ' .urlencode( $wd ); $data = array (); $header = array (); $response = docurl( $url , $data , $header , 5); echo $response ; ?> |
输出后发现有部分图片不能显示
2.采集后的图片不显示原因分析
直接在百度中搜寻,页面是可以显示图片的。使用firebug查看图片路径,发现采集的图片域名与在百度搜寻的图片域名不同。
采集返回的图片域名 t11.baidu.com
正常搜寻的图片域名 ss1.baidu.com
查看采集与正常搜寻的html,发现有个域名转换的js是不一样的
采集
1
|
2
3
4
5
6
7
8
9
10
11
12
|
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 " , }; |
正常搜寻
1
|
2
3
4
5
6
7
8
9
10
11
12
|
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 " , }; |
因此可以断定是,百度根据来源地址、ip、header等参数,判断如果是采集的,则返回不同的js。
3.采集后图片不显示的解决方法
把采集到的html,根据定义的域名做一次批量转换即可。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<?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 ; } // 域名转换 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 = '仙剑奇侠传' ; $url = ' http://www.baidu.com/s?wd= ' .urlencode( $wd ); $data = array (); $header = array (); $response = docurl( $url , $data , $header , 5); echo cdomain( $response ); // 调用域名转换 ?> |
增加域名转换后,所有的图片都可以正常显示。
以上所述是小编给大家介绍的php curl采集百度搜寻结果图片不显示问题的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/fdipzone/article/details/54604708