Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Search autocomplete API?

Does Google provide API access to autocomplete for search like on the actual site? I have not been able to find anything.

I would like to use Google's autocomplete logic for web search on my own site which relies on Google's search API.

like image 280
Justin Meltzer Avatar asked Jun 21 '11 16:06

Justin Meltzer


People also ask

Is Google address autocomplete API free?

Autocomplete without Place Details – Per Session (price starting at 0.017 USD per session)

What is Google autocomplete API?

If you're building a client-side application, take a look at the Places SDK for Android, the Places SDK for iOS, and the Places Library, Maps JavaScript API. The Place Autocomplete service is a web service that returns place predictions in response to an HTTP request.


2 Answers

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

like image 77
RickyA Avatar answered Sep 23 '22 15:09

RickyA


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/

like image 36
yaphi1 Avatar answered Sep 19 '22 15:09

yaphi1