How do you perform a search query on Google using Python? How do you store the search results in a Microsoft Word document?
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With