获取某关键词在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编码啊。

解决方案 »

  1.   

    是可以的,而且也能正确得到结果。
    貌似直接输入地址和file_get_contents得到的结果有点不一样。
    你输出看一下就知道了。你的问题主要在file_get_contents这个函数,file_get_contents不是很好用,经常会得不到结果。
    建议用fscoket或curl来获取信息。
      

  2.   

    file_get_contents
    对于那些没有任何限制,GET方式发送表单的页面有效。
    获取的时候,是不发送任何cookie的。
      

  3.   

    不是utf-8的,,,,,,,是 gbk 转成 utf-8,,,我试过就可以了
      

  4.   

    什么意思?$contents字符串不是utf-8的吗?具体如何修改才能成功?恳请指点清楚,谢谢了!
      

  5.   

    编码转化一下就可以了<?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);
    $contents = mb_convert_encoding($contents,'utf-8','gbk');
    preg_match_all('~获得约(.*?)条结果~is',$contents,$p);
    print_r($p[1]);
    ?>
      

  6.   

    感谢5楼,成功的解决了问题。不过还是有个疑问,google搜索页面已经是utf-8编码了,$contents也应该是utf-8,为什么还有个从gbk转到utf-8的过程?
      

  7.   

    我运行的时候 这一行报错  $contents=file_get_contents($url);
     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 ( ) 如何解决