Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google search with Python [duplicate]

How do you perform a search query on Google using Python? How do you store the search results in a Microsoft Word document?

like image 481
Hick Avatar asked Dec 20 '10 15:12

Hick


2 Answers

Use the provided API. First register to get an API key here. Then you can use Python's urllib2 package to fetch the results, e.g.

import urllib2
import json
import pprint
data = urllib2.urlopen('https://www.googleapis.com/customsearch/v1?key=YOUR_KEY_HERE&cx=017576662512468239146:omuauf_lfve&q=lectures')
data = json.load(data)
pprint.PrettyPrinter(indent=4).pprint(data['items'][0]) # Print the raw content of the first result

Which outputs

{   'cacheid': 'TxVqFzFZLOsJ',
    'displayLink': 'www.stanford.edu',
    'htmlSnippet': 'Apr 7, 2010 \\u003cb\\u003e...\\u003c/b\\u003e Course materials. \\u003cb\
\u003eLecture\\u003c/b\\u003e slides \xc2\xb7 \\u003cb\\u003eLecture\\u003c/b\\u003e videos (2
008) \xc2\xb7 Review sessions. \\u003cbr\\u003e  Assignments. Homework \xc2\xb7 Reading. Exams
. Final exam \\u003cb\\u003e...\\u003c/b\\u003e',
    'htmlTitle': 'EE364a: \\u003cb\\u003eLecture\\u003c/b\\u003e Videos',
    'kind': 'customsearch#result',
    'link': 'http://www.stanford.edu/class/ee364a/videos.html',
    'snippet': 'Apr 7, 2010 ... Course materials. Lecture slides \xc2\xb7 Lecture videos (2008
) \xc2\xb7 Review sessions.   Assignments. Homework \xc2\xb7 Reading. Exams. Final exam ...',
        'title': 'EE364a: Lecture Videos'}

Please make sure to replace YOUR_KEY_HERE with your key.

To create an MS Word document from Python, read this question.

like image 199
moinudin Avatar answered Oct 15 '22 03:10

moinudin


http://code.google.com/apis/customsearch/v1/getting_started.html

http://code.google.com/apis/customsearch/v1/using_rest.html

Google's custom search API looks to be what you're looking for. You'll need to get a API key first; then it seems they let you do up to 100 searches a day.

Use urllib2 to fetch the URL, and simplejson to decode it. (Google around for these packages if you don't already have them.) You can use json.load() to turn the response into a python dictionary that you can easily read from. Happy hacking!

Edit: As for creating the word document, you have a variety of options, detailed here: How can I create a Word document using Python?

like image 3
Aphex Avatar answered Oct 15 '22 02:10

Aphex