Google Search autocomplete API?

前端 未结 5 1588
执念已碎
执念已碎 2020-11-27 10:26

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

相关标签:
5条回答
  • 2020-11-27 10:45

    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

    0 讨论(0)
  • 2020-11-27 10:45

    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/

    0 讨论(0)
  • 2020-11-27 10:50

    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.

    0 讨论(0)
  • 2020-11-27 10:54

    What are you trying to use an auto-complete for? More information would help narrow it down.

    As far as I know, google does not provide one, but they do exist like jQuery UI's auto-complete.

    EDIT:

    If you are using their custom search API view here for autocomplete.

    0 讨论(0)
  • 2020-11-27 11:09

    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 
    
    0 讨论(0)
提交回复
热议问题