7

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
Hick
  • 35,524
  • 46
  • 151
  • 243
  • 1
    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
  • 2
    For future, please note that you should ask these as two separate questions as they aren't very related at all. – moinudin Dec 20 '10 at 15:42

3 Answers3

16

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.

Community
  • 1
  • 1
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • 2
    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 http://stackoverflow.com/questions/1083250/running-json-through-pythons-eval – user9876 Dec 20 '10 at 15:49
  • 2
    Yes, why use `eval()` when you can use the much safer `json.load()`? – Aphex Dec 20 '10 at 15:51
  • 2
    @user9876 @Aphex Changed in the answer. I'm not very familiar with json processing in Python, so thanks for pointing that out. – moinudin Dec 20 '10 at 15:59
3

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?

Community
  • 1
  • 1
Aphex
  • 7,390
  • 5
  • 33
  • 54
0

One Question for the price of two Questions:

First - You Want to perform a search query on Google using with Python Languages.

Second - You want to save the search results into a Microsoft Word document.

Hi, I like Autohotkey Tools with Python Languages.

If you like to, make Computer Movements Scripts With Keyboard Shortcuts Macros.

You can try to use AutoPythonLauncher Software on your Windows System. For more info Click Here or look to this Youtube Video - you can see what it can do.

With this Tool you can Make (Toolbars) - and Create a Set of Clickable Pictures with Python Command Scripts. (Keyboard Shortcuts Combinations)

1 - Answer the First Question:

With this code, You can Select any text and do direct a GOOGLE SEARCH with query parameters (example:us,50 results)

With AutoPythonLauncher you can Choose a Picture and Write in the Command Editor this Python Code.

Save and Restart AutoPythonLauncher and you can use it as a LAUNCHER. Select any text and Click With your MOUSE or TOUCH device on that Picture, and it is Done.

# US - SEARCH
# Firefox Mozilla Browser - Chrome Browser - Internet Explorer Browser - Microsoft Edge Browser 
if WindowExists("MozillaWindowClass") or WindowExists("Chrome_WidgetWin_1")  or WindowExists("CLASS:IEFrame"):
    pyautogui.hotkey('ctrl', 'c') #copy the selected text to clipboard 1 memory
    time.sleep(0.2)    #wait 0.2 seconds
    pyautogui.hotkey('ctrl', 't') # CTRL+t make a new tab + goto address bar  - use CTRL+L for the active tab + goto address bar
    time.sleep(0.2)    #wait 0.2 seconds
    texta = "https://www.google.com/search?q="
    a = tk.Tk()
    textb = a.clipboard_get() # read the clipboard memory and put in variable textb
    textc = "&lr=lang_us&hl=us&num=50" # google parameters [us - United States]
    pyautogui.typewrite(texta + textb + textc)
    pyautogui.hotkey('enter') 

2 - Answer the Second Question:

Choose a Picture and Write in the Command Editor this Python Code. With this you can then save the (example:us 50 results) to a file.

import pyautogui
import time
time.sleep(.750)
pyautogui.hotkey('Ctrl','a') #select all
time.sleep(.750)
pyautogui.hotkey('Ctrl','c') #Copy to clipboard Memory
time.sleep(.750)


#run notepad - If you want to use wordpad you can change the code a litte bid
#######################
import pywinauto
pywinauto.Application().start(r"c:\windows\system32\notepad.exe")
#######################

#this will put All the text into notepad
#######################
time.sleep(2)
pyautogui.hotkey('Ctrl','v') #paste the clipboard Memory. 
#######################

#Save a File - "Save as..." 
#######################
time.sleep(2)
pyautogui.hotkey('Alt','f','a') #Many Programs use Shortcut Alt+f+a to "Save as..." 
time.sleep(.750)
pyautogui.typewrite('c:\\test\\test.txt',0)
time.sleep(2)
pyautogui.hotkey('enter')
#######################
stevecody
  • 658
  • 1
  • 6
  • 18
  • Oh, you use pywinauto to start the app, but nothing more. For this simple case `subprocess.Popen("notepad.exe")` is enough. But pywinauto has much more powerful functions doing automatic waiting for opening a window. So you don't have to add hard-coded `time.sleep` calls. Don't know why you use only 1% of the Python power. – Vasily Ryabov Nov 30 '17 at 03:34
  • @ Vasily Ryabov - 1 - pywinauto packages you can do many things more then only start the app that is the reason why i did choose for this Command. 2 - And i now you can replace time.sleep(2) into **pywinauto.Application().WindowSpecification.Wait('enabled')** (wait until **Exist** Notepad Window) - but still this does not work i did try out, the computer need a little bid more time to **exist** and then **activate** the Notepad Window -- and for beginners it is simple to explain the command time.sleep(2) - and the users can always simple change the sleep times to speedup the Macro Movements. – stevecody Dec 01 '17 at 12:31
  • It could be fine to explain non-working scenario in more details. We made some fixes to `.wait` method recently (in 0.6.3). So I'm surprised it's still not working. – Vasily Ryabov Dec 02 '17 at 07:42
  • Also `.wait(...)` is always faster than `time.sleep` because timeout is just a maximum time to wait. Regarding `exists` + `enabled` we're thinking to re-design it in future big release next summer. So finding the element and checking its state will be separate. – Vasily Ryabov Dec 02 '17 at 07:44