How to query an advanced search with google customsearch API?

前端 未结 3 1233
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 14:53

How can I programmatically using the Google Python client library do an advanced search with Google custom search API search engine in order to return a list of first n

相关标签:
3条回答
  • 2021-02-05 15:08

    An alternative using the python requests library if you do not want to use the google discovery api:

    import requests, pprint
    q='italy'
    api_key='AIzaSyCs.....................'
    
    q = requests.get('https://content.googleapis.com/customsearch/v1', 
        params={ 'cx': '013027958806940070381:dazyknr8pvm', 'q': q, 'key': api_key} )
    pprint.pprint(q.json())
    
    0 讨论(0)
  • 2021-02-05 15:22

    First you need to define a custom search as described here, then make sure your my_cse_id matches the google API custom search (cs) id, e.g.

    cx='017576662512468239146:omuauf_lfve'
    

    is a search engine which only searches for domains ending with .com.

    Next we need our developerKey.

    from googleapiclient.discovery import build
    service = build("customsearch", "v1", developerKey=dev_key)
    

    Now we can execute our search.

    res = service.cse().list(q=search_term, cx=my_cse_id).execute()
    

    We can add additional search parameters, like language or country by using the arguments described here, e.g.

    res = service.cse().list(q="the best dog food", cx=my_cse_id, cr="countryUK", lr="lang_en").execute()
    

    would serch for "the best dog food" in English and the site needs to be from the UK.


    The following modified code worked for me. api_key was removed since it was never used.

    from googleapiclient.discovery import build
    
    my_cse_id = "012156694711735292392:rl7x1k3j0vy"
    dev_key = "<Your developer key>"
    
    def google_search(search_term, cse_id, **kwargs):
        service = build("customsearch", "v1", developerKey=dev_key)
        res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
        return res['items']
    
    results = google_search('boxer dogs', my_cse_id, num=10, cr="countryCA", lr="lang_en")
    for result in results:
        print(result.get('link'))
    

    Output

    http://www.aboxerworld.com/whiteboxerfaqs.htm
    http://boxerrescueontario.com/?section=available_dogs
    http://www.aboxerworld.com/abouttheboxerbreed.htm
    http://m.huffpost.com/ca/entry/10992754
    http://rawboxers.com/aboutraw.shtml
    http://www.tanoakboxers.com/
    http://www.mondlichtboxers.com/
    http://www.tanoakboxers.com/puppies/
    http://www.landosboxers.com/dogs/puppies/puppies.htm
    http://www.boxerrescuequebec.com/
    
    0 讨论(0)
  • 2021-02-05 15:30

    This is late but hopefully it helps someone...

    For advanced search use

    response=service.cse().list(q="mysearchterm", 
    cx="017576662512468239146:omuauf_lfve", ).execute()
    

    The list() method takes in more args to help advance your search... check args here: https://developers.google.com/custom-search/json-api/v1/reference/cse/list

    0 讨论(0)
提交回复
热议问题