当用户在 Google 搜索页面上开始输入搜索请求时,Google 会显示一个下拉菜单,其中包含一些流行的补全建议。那里通常有一些有用或有趣的内容。
没有 GUI 的情况下我怎样才能获取该列表?
期望类似这样的事情:
$ curl https://www.google.com/.... "How do I" ... | ...
how do I love thee
how do I get a passport
how do I craft this again
how do I live without you
答案1
使用这个网址仍然对我有用:
http://suggestqueries.google.com/complete/search?client=firefox&q=how+do+i
您将获得一个 JSON 文件,您可以解析它以获取完成建议。
有一些选项(例如回调等),但对于您来说,这个 URL 就足以达到其目的。
结果(我自己添加了换行符,因此您需要解析这个 JSON):
["how do i",
["how do i craft this again",
"how do i live without you",
"how do i know if i have 32 or 64 bit windows 7",
"how do i live",
"how do i breathe lyrics",
"how do i look bald",
"how do i download youtube videos",
"how do i look with a beard",
"how do i know if i have 32 or 64 bit windows 8",
"how do i live leann rimes"
]
]
您还可以创建一个小的 php 文件
<?php
$url = sprintf("http://suggestqueries.google.com/complete/search?client=firefox&q=%s", urlencode("how do i"));
$json = file_get_contents($url);
$results = json_decode(utf8_encode($json));
print_r($results);
结果php myfile
是:
Array
(
[0] => how do i
[1] => Array
(
[0] => how do i love thee
[1] => how do i get a passport
[2] => how do i craft this again
[3] => how do i live without you
[4] => how do i renew my passport
[5] => how do i live lyrics
[6] => how do i put this gently
[7] => how do i print screen
[8] => how do i enable cookies
[9] => how do i take a screenshot
)
)
(我不确定为什么结果不同,但这可能与谷歌可以检测您正在使用的客户端/用户代理并相应地给出结果有关)