Take the 2-minute tour ×

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

share|improve this question
Assuming you want to actually know how to do it, you've got at least two separate questions here: 1. How do you "search a certain input over google using python" (I assume you mean "search for", not "search". ie: the "certain input" is the query, not the corpus, right?). 2. How do you store information in a "doc" file (a Microsoft Word .doc file, or something else?). –  Laurence Gonsalves Dec 20 '10 at 15:29
For future, please note that you should ask these as two separate questions as they aren't very related at all. –  marcog Dec 20 '10 at 15:42
add comment

3 Answers

up vote 3 down vote accepted

See this question

Google Search from a Python App

Contains answer from Alex Martelli (python 2.6) and a python 3 port as well. You should be able to modify it accordingly. It uses the json and urllib that @Aphex mentions

share|improve this answer
It seems to me the answers there use the now-deprecated Web search API: <code.google.com/apis/websearch/>; the other answers here use the current custom search API. –  John Y Dec 20 '10 at 16:10
@JohnY - yes but the Custom Search API requires you to configure which sites you want to search across - how do you set it up to search the entire web, not just a subset? –  Day Dec 27 '11 at 16:48
@Day: I don't know. I haven't used it. The last time I read anything about it, it didn't seem as easy as it should be. I think Google is trying to discourage people from using their full-Web search, but I don't know the reason for that either. –  John Y Dec 28 '11 at 2:13
@Day (and anyone else, really): There is now an answer at this SO question which gives steps for using Google's custom search as a full-Web search. Fair warning: I haven't tried it myself. –  John Y Nov 9 '12 at 16:48
add comment

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.

share|improve this answer
This answer is dangerous. It will break on certain (rare) responses, and it contains a remote-code-execution security bug. (marcog: I know you've put the "Beware eval()ing untrusted data!" comment there. But there are plenty of people who will ignore that warning because "it works, why should I change it". Then they will write dangerous code). See also stackoverflow.com/questions/1083250/… –  user9876 Dec 20 '10 at 15:49
Yes, why use eval() when you can use the much safer json.load()? –  Aphex Dec 20 '10 at 15:51
@user9876 @Aphex Changed in the answer. I'm not very familiar with json processing in Python, so thanks for pointing that out. –  marcog Dec 20 '10 at 15:59
Thanks for fixing it! –  user9876 Dec 20 '10 at 17:21
add comment



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?

share|improve this answer
add comment

Your Answer


