Php 如何从Google查询中获取结果数
当我试图开发自己的关键字难度工具时,在从谷歌查询中获取结果总数时遇到了一个问题。为了尽可能清楚,这里有一个屏幕,显示了我要获取的号码: 我在谷歌自定义搜索API上找不到任何关于如何实现这一点的参考资料,因此我构建了一个小型刮板,但我觉得这不是实现这一点的最佳方法。这是我的密码:Php 如何从Google查询中获取结果数,php,google-api,Php,Google Api,当我试图开发自己的关键字难度工具时,在从谷歌查询中获取结果总数时遇到了一个问题。为了尽可能清楚,这里有一个屏幕,显示了我要获取的号码: 我在谷歌自定义搜索API上找不到任何关于如何实现这一点的参考资料,因此我构建了一个小型刮板,但我觉得这不是实现这一点的最佳方法。这是我的密码: <?php $url = "http://www.google.com/search?q=".$keyword; $text = file_get_contents($url); //get string betw
<?php
$url = "http://www.google.com/search?q=".$keyword;
$text = file_get_contents($url);
//get string between 2 strings function
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
//fetch the string between "About" and "results"
$res = get_string_between($text, "About", "results");
//keep only numeric characters
$res = preg_replace('/[^0-9]+/', '', $res);
?>
你能建议一个更好的方法吗?(最好使用Google API)使用正则表达式:
$params = array('q' => 'shark with lasers that shoot monkeys with balloons on skyscrapers please give me less results asdafasddfg');
$content = file_get_contents('http://www.google.com/search?' . http_build_query($params));
preg_match('/About (.*) results/i', $content, $matches);
echo !empty($matches[1]) ? $matches[1] : 0;
// output: 14,300
我在他们的API中找不到任何东西。注意,这是针对谷歌TOS的。Bing的API将为您提供一个结果计数。我以前使用过这种刮取方法,但我要提醒您不要这样做。谷歌经常改变他们结果页面的标记(我猜部分是因为他们试图阻止这种做法)。这基本上是对谷歌IP的侵犯。此外,结果页面有时对同一个“块”有不同的标记。我相信返回结果的数量有不同的标记,这取决于返回结果的数量(
none
vs10
vs10000
,等等)。这个数字太夸张了,我不会使用它。有没有想过结果的“最后”一页?@njk我同意你的看法,这不是一个非常准确的指标,但我也在尝试获取allintitle:“关键字”和Allianchor:“关键字”查询的结果数。在评估关键词难度时,我主要关注这两个查询的结果数量。我在某个地方读到bing通过其API提供了这个数字,但我注意到谷歌与bing在同一查询中的结果总数之间存在巨大差异。。。