Google 搜索自动完成 API?

发布于 2024-11-16 17:56:00 字数 1808 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷爱 2024-11-23 17:56:00

新网址为:

http://suggestqueries.google.com/complete/ search?client=firefox&q=YOURQUERY

客户端部分是必需的;我没有测试其他客户端。

[编辑]

如果您想要回调,请使用以下命令:

http: //suggestqueries.google.com/complete/search?client=chrome&q=YOURQUERY&callback=callback

@Quandary 发现;该回调不适用于客户端“firefox”。

[EDIT2]

正如 @ user2067021 所示,此 api 将在 2015 年 10 月 8 日停止工作:自动完成 API 更新

The new url is:

http://suggestqueries.google.com/complete/search?client=firefox&q=YOURQUERY

the client part is required; I did't test other clients.

[EDIT]

If you want the callback use this:

http://suggestqueries.google.com/complete/search?client=chrome&q=YOURQUERY&callback=callback

As @Quandary found out; the callback does not work with client "firefox".

[EDIT2]

As indicated by @ user2067021 this api will stop working as of 10-08-2015: Update on the Autocomplete API

青丝拂面 2024-11-23 17:56:00

上面提到的大多数方法都适合我,具体来说以下方法符合我的目的。

http://suggestqueries.google.com/complete/search?client=firefox&q=YOURQUERY

作为网络编程的新手,我不太了解“回调”功能和查询返回的文件的格式。我对 AJAX 和 JSON 知之甚少。
有人可以提供有关查询返回的文件格式的更多详细信息吗?
谢谢。

Most of the above mentioned methods works for me, specifically the following serves my purpose.

http://suggestqueries.google.com/complete/search?client=firefox&q=YOURQUERY

Being a newbie in web programming, I'm not much aware of the "Callback" functionality and the format of the file returned by query. I'm little aware of AJAX and JSON.
Could someone provide more details about the format of file returned by the query.
Thanks.

美人如玉 2024-11-23 17:56:00

首先,转到谷歌,单击设置(右下角),将搜索设置更改为“从不显示即时结果”。这样,您将获得定期自动完成而不是整页即时结果。

保存设置后,返回打开浏览器的开发人员工具并转到“网络”选项卡。如果您使用的是 Firefox,则可能需要

在搜索框中重新输入一个字母。“网络”中应该会出现一个新行。您刚刚打开的窗口正在显示。自动完成数据的来源。它应该看起来像这样:

https://www.google.com/complete/search?client=hp&hl=en&sugexp=msedr&gs_rn=62&gs_ri=hp&cp=1&gs_id=9c&q=a&xhr=t&callback=hello

”部分后面注意到您的搜索词。

您会在“Add &callback=myAmazingFunction 您可以将 myAmazingFunction 替换为您想要处理数据的函数的名称。

是显示搜索词“a”的自动完成数据所需的代码示例。

<div id="output"></div>

<script>
/* this function shows the raw data */
function myAmazingFunction(data){
    document.getElementById('output').innerHTML = data;
}
</script>

<script src="https://www.google.com/complete/search?client=hp&hl=en&sugexp=msedr&gs_rn=62&gs_ri=hp&cp=1&gs_id=9c&q=a&xhr=t&callback=hello&callback=myAmazingFunction"></script>

下面 如果您知道如何获取数据,下一步就是自动更改最后一个脚本(带有自动完成 url 的脚本)。基本过程是:每次用户在搜索框 (onkeyup) 中键入内容时,替换 url 中的搜索词 (q=whatever),然后将带有该 url 的脚本附加到正文。删除之前的脚本,以免正文变得混乱。

有关详细信息,请参阅 http://simplestepscode.com/autocomplete-data-tutorial/

First, go to google, click Settings (bottom right corner), change Search Settings to "never show instant results. That way, you'll get regular autocomplete instead of a full page of instant results.

After your settings are saved, go back to the Google main home page. Open your browser's developer tools and go to the Network tab. If you're in Firefox, you might have to reload the page.

Type a letter in the search box. A new line should appear in the Network window you just opened. That line is showing where the autocomplete data came from. Copy that url. It should look something like this:

https://www.google.com/complete/search?client=hp&hl=en&sugexp=msedr&gs_rn=62&gs_ri=hp&cp=1&gs_id=9c&q=a&xhr=t&callback=hello

You'll notice your search term right after the part that says q=.

Add &callback=myAmazingFunction to the end of the url. You may replace myAmazingFunction with whatever you want to name your function that will handle the data.

Here's an example of the code required to show the autocomplete data for the search term "a".

<div id="output"></div>

<script>
/* this function shows the raw data */
function myAmazingFunction(data){
    document.getElementById('output').innerHTML = data;
}
</script>

<script src="https://www.google.com/complete/search?client=hp&hl=en&sugexp=msedr&gs_rn=62&gs_ri=hp&cp=1&gs_id=9c&q=a&xhr=t&callback=hello&callback=myAmazingFunction"></script>

Now that you know how to get the data, the next step is to automatically change that last script (the one with the autocomplete url). The basic procedure is: each time the user types something in the search box (onkeyup) replace the search term (q=whatever) in the url, and then append to the body a script with that url. Remove the previous script so that the body doesn't get cluttered.

For more info, see http://simplestepscode.com/autocomplete-data-tutorial/

晚风撩人 2024-11-23 17:56:00

您好,我不知道这个答案是否与您相关,但谷歌通过以下 get 请求返回 JSON 数据(尽管这不是官方 API,但许多工具栏正在使用此 API,因此谷歌没有理由停止它):

http://google.com/complete/search?q=<Your keywords here>&hl=en 

Hi I don't know if this answer is relevant for you anymore or not but google returns JSON data through following get request (although this isn't an official API but many toolbars are using this API so there's no reason why google might discontinue it):

http://google.com/complete/search?q=<Your keywords here>&hl=en 
动听の歌 2024-11-23 17:56:00

您应该使用 AutocompleteService 并将该文本框值传递到 service.getPlacePredictions 函数中。它在回调函数中发送数据。

let service = new google.maps.places.AutocompleteService();

let displaySuggestions = function(predictions, status) {
}

service.getPlacePredictions({
    input: value
}, displaySuggestions);

基础:https://developers.google。 com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteService.getPlacePredictions

示例:https://dzone.com/articles/implement-and-optimize-使用-google-pl 自动完成

You should use AutocompleteService and pass that text box value into the service.getPlacePredictions function. It send the data in callback function.

let service = new google.maps.places.AutocompleteService();

let displaySuggestions = function(predictions, status) {
}

service.getPlacePredictions({
    input: value
}, displaySuggestions);

Base: https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteService.getPlacePredictions

example: https://dzone.com/articles/implement-and-optimize-autocomplete-with-google-pl

凉栀 2024-11-23 17:56:00

您可以使用关键字 Quill,它提供 Google 自动完成 API。免费获取 API 密钥并向端点发出请求。

用于查找短语“空气炸锅”的自动完成关键字的 JS 示例如下:

const data = JSON.stringify({
  "keyword": "air fryers",
  "gl": "US",
  "hl": "en"
});

fetch('https://api.keywordquill.com/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-KEY': 'API KEY'
  },
  body: data
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

You can use Keyword Quill, which offers a Google autocomplete API. Get an API key for free and make a request to the endpoint.

A JS example for finding autocompleted keywords for the phrase "air fryers" would be:

const data = JSON.stringify({
  "keyword": "air fryers",
  "gl": "US",
  "hl": "en"
});

fetch('https://api.keywordquill.com/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-KEY': 'API KEY'
  },
  body: data
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文