获取某关键词在google中的搜索结果条目数<?php
header("Content-type:text/html;charset=utf-8");
$url="http://www.google.com.hk/search?hl=zh-CN&source=hp&q=%E5%8D%97%E9%9D%9E%E4%B8%96%E7%95%8C%E6%9D%AF&aq=f&aqi=g10&aql=&oq=&gs_rfai=";
$contents=file_get_contents($url);
preg_match_all('~获得约(.*?)条结果~is',$contents,$p);
print_r($p[1]);
?>为什么匹配不成功,输出没有结果?google搜索页面是utf-8编码啊。
header("Content-type:text/html;charset=utf-8");
$url="http://www.google.com.hk/search?hl=zh-CN&source=hp&q=%E5%8D%97%E9%9D%9E%E4%B8%96%E7%95%8C%E6%9D%AF&aq=f&aqi=g10&aql=&oq=&gs_rfai=";
$contents=file_get_contents($url);
preg_match_all('~获得约(.*?)条结果~is',$contents,$p);
print_r($p[1]);
?>为什么匹配不成功,输出没有结果?google搜索页面是utf-8编码啊。
貌似直接输入地址和file_get_contents得到的结果有点不一样。
你输出看一下就知道了。你的问题主要在file_get_contents这个函数,file_get_contents不是很好用,经常会得不到结果。
建议用fscoket或curl来获取信息。
对于那些没有任何限制,GET方式发送表单的页面有效。
获取的时候,是不发送任何cookie的。
header("Content-type:text/html;charset=utf-8");
$url="http://www.google.com.hk/search?hl=zh-CN&source=hp&q=%E5%8D%97%E9%9D%9E%E4%B8%96%E7%95%8C%E6%9D%AF&aq=f&aqi=g10&aql=&oq=&gs_rfai=";
$contents=file_get_contents($url);
$contents = mb_convert_encoding($contents,'utf-8','gbk');
preg_match_all('~获得约(.*?)条结果~is',$contents,$p);
print_r($p[1]);
?>
Warning: file_get_contents(http://www.google.com.hk/search?hl=zh-CN&source=hp&q=%E5%8D%97%E9%9D%9E%E4%B8%96%E7%95%8C%E6%9D%AF&aq=f&aqi=g10&aql=&oq=&gs_rfai=) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in D:\xampp\xampp\htdocs\Untitled-1.php on line 11
Array ( ) 如何解决