加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:45:28 所属栏目:PHP教程 来源:网络整理
导读:虽然在从Google查询中获取结果总数时尝试开发自己的关键字难度工具时遇到了问题.为了使它尽可能清晰,这里是我想要获取的数字的屏幕: 我在Google自定义搜索API上找不到任何关于如何执行此操作的参考资料,所以我已经构建了一个小型的刮刀,但我觉得这不是最好
虽然在从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将为您提供结果计数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读