pythonpython-requestsgoogle-searchgoogle-search-api

google search with python requests library


(I've tried looking but all of the other answers seem to be using urllib2)

I've just started trying to use requests, but I'm still not very clear on how to send or request something additional from the page. For example, I'll have

import requests

r = requests.get('http://google.com')

but I have no idea how to now, for example, do a google search using the search bar presented. I've read the quickstart guide but I'm not very familiar with HTML POST and the like, so it hasn't been very helpful.

Is there a clean and elegant way to do what I am asking?


Solution

  • Request Overview

    The Google search request is a standard HTTP GET command. It includes a collection of parameters relevant to your queries. These parameters are included in the request URL as name=value pairs separated by ampersand (&) characters. Parameters include data like the search query and a unique CSE ID (cx) that identifies the CSE that is making the HTTP request. The WebSearch or Image Search service returns XML results in response to your HTTP requests.

    First, you must get your CSE ID (cx parameter) at Control Panel of Custom Search Engine

    Then, See the official Google Developers site for Custom Search.

    There are many examples like this:

    http://www.google.com/search?
      start=0
      &num=10
      &q=red+sox
      &cr=countryCA
      &lr=lang_fr
      &client=google-csbe
      &output=xml_no_dtd
      &cx=00255077836266642015:u-scht7a-8i
    

    And there are explained the list of parameters that you can use.