Return Google Search To Python

I am trying to get the first 20 Google search results. When I use urllib2.urlopen(), it gives me an error and says that I am forbidden. I heard this has something to do with faking your user agent string, but I have no experience with urllib2, and I would really appreciate it if anyone could help.

Thank giodamelio

+3
source share
3 answers

You should probably just use a library that does all the hard work.

xGoogle allows you to get search results in a list

From the examples

from xgoogle.search import GoogleSearch
gs = GoogleSearch("quick and dirty")
gs.results_per_page = 50
results = gs.get_results()
+2
source

: API Google xGoogle.

API- Gooogle

Google JSON\Atom API , . , , . ( Google):

GET https://www.googleapis.com/customsearch/v1?
            key=INSERT-YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=lectures

JSON, Python.

xGoogle

xgoogle package (. ) (, , ) Google, .

, xGoogle. , , 15 API.

+2
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
+2
source

Source: https://habr.com/ru/post/1778681/


All Articles