php – 如何从Google查询中获取结果数量

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何从Google查询中获取结果数量脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
虽然在从GOOGLE查询获取结果总数时尝试开发自己的关键字难度工具时遇到了问题.为了使它尽可能清晰,这里是我想要获取的数字的屏幕:

我在Google自定义搜索API上找不到任何关于如何执行此操作的参考资料,所以我已经构建了一个小型的刮刀,但我觉得这不是最好的方法.这是我的代码

<?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中找不到任何东西.请注意,这是针对Google TOS的. Bing的API将为您提供结果计数.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何从Google查询中获取结果数量全部内容,希望文章能够帮你解决php – 如何从Google查询中获取结果数量所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。