Google搜索:使用PHP抓取结果页面以获得总结果

时间:2010-04-01 13:05:48

标签: php

是否可以使用PHP抓取Google搜索结果页以提取找到的搜索结果总数?

如果是这样,我该如何做呢?

由于

4 个答案:

答案 0 :(得分:6)

使用phpsimplehtmlparser

尝试此操作
$search_query = 'google';
$url = sprintf('http://www.google.com/search?q=%s', $search_query);
$html = file_get_html($url);
$results = $html->find('#resultStats/b', 2)->innertext;

echo sprintf('Google found %s results for "%s"', $results, $search_query);

答案 1 :(得分:5)

这个PHP类做到了:http://www.phpclasses.org/browse/package/3924.html

  

“这个类可以用来获得   给定的结果总数   谷歌搜索查询。

     

它访问Google搜索网站   执行给定搜索的查询   术语

     

该类解析结果页面和   提取结果总数   给定的搜索查询返回。“

答案 2 :(得分:2)

根据您计划发送的请求数量,您需要一堆代理。 您可以每天发送大约500个请求和IP /代理,而不会造成麻烦或被检测到。

你应该阅读google-rank-checker.squabbel.com文章,它包含一个PHP的全功能刮刀。使用该刮刀,根据您的要求进行修改,并添加phpsimplehtmlparser的代码(另一个答案),以获取关键字的总点击信息。

我建议使用libCURL访问Google本身。 与使用更简单的API相比,你将有更多的选择,你不会对file_get_html()或类似的php内部函数感兴趣,因为Google会很快阻止你的脚本。

这样的事情:

  curl_setopt ($ch, CURLOPT_HEADER, 0);
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER , 1);
  $curl_proxy = "$IP:$PORT";
  curl_setopt($ch, CURLOPT_PROXY, $curl_proxy);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en; rv:1.9.0.4) Gecko/2009011913 Firefox/3.0.6");
  $url = sprintf('http://www.google.com/search?q=%s', $keyword);
  curl_setopt ($ch, CURLOPT_URL, $url);
  $htmdata = curl_exec ($ch);

现在只需使用regex()/ substr()/ strstr()从$ htmldata中获取数据

答案 3 :(得分:1)

我使用这个php脚本在google的搜索中查找我的名字的总结果。

<?php
$homepage = file_get_contents('http://www.google.co.in/search?ix=nh&sourceid=chrome&ie=UTF-8&q=Mohit+dabas');
preg_match('/(About )?([\d,]+) result/si', $homepage, $p) ;
echo $p[0];
?>

要注意的主要事项是上面定义的路径中的'&amp; q'参数

我的名字包含空格,因此浏览器为其添加了“+”

因此,您应该检查您的查询(即&amp; q)参数以及您的查询是否包含一些特殊的

字符,如。,:,%等,那么您应该注意浏览器如何处理它们

更改参数acc。你需要的int脚本。

为可怜的英语而苦恼