用Python搜索谷歌

时间:2010-12-20 15:18:50

标签: python ms-word google-api

如何使用Python在Google上执行搜索查询?如何将搜索结果存储在Microsoft Word文档中?

4 个答案:

答案 0 :(得分:16)

使用provided API。首先注册以获取API密钥here。然后,您可以使用Python的urllib2包来获取结果,例如

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

哪个输出

{   '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'}

请务必使用 键替换YOUR_KEY_HERE

要从Python创建MS Word文档,请阅读this question

答案 1 :(得分:3)

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

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

Google的自定义搜索API看起来就像您正在寻找的那样。您需要先获得API密钥;那么它们似乎让你每天最多进行100次搜索。

使用urllib2获取网址,使用simplejson对其进行解码。 (如果您还没有这些软件包,可以使用Google。)您可以使用json.load()将响应转换为可以轻松读取的python字典。快乐的黑客!

修改:至于创建word文档,您有多种选项,详情请参阅:How can I create a Word document using Python?

答案 2 :(得分:3)

看到这个问题

  

Google Search from a Python App

包含来自Alex Martelli(python 2.6)和python 3端口的答案。 您应该能够相应地修改它。 它使用@Aphex提到的json和urllib

答案 3 :(得分:0)

两个问题价格的一个问题:

首先 - 您希望使用Python语言在Google上执行搜索查询。

第二 - 您希望将搜索结果保存到Microsoft Word文档中。

嗨,我喜欢使用Python语言的Autohotkey工具。

如果您愿意,请使用键盘快捷键宏制作计算机移动脚本。

您可以尝试在Windows系统上使用AutoPythonLauncher软件。有关详细信息,请Click Here 或者看看这个Youtube Video - 你可以看到它能做什么。

使用此工具,您可以制作(工具栏) - 并使用Python命令脚本创建一组可点击图片。 (键盘快捷键组合)

1 - 回答第一个问题:

使用此代码,您可以选择任何文字并使用查询参数指定GOOGLE SEARCH(例如:我们,50个结果)

使用AutoPythonLauncher,您可以在命令编辑器中选择图片和写入此Python代码。

保存并重新启动AutoPythonLauncher,您可以将其用作启动器。 选择任何文本,然后单击该图片上的“使用鼠标或触摸设备”,它就完成了。

# 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 - 回答第二个问题:

选择图片并在命令编辑器中编写此Python代码。 有了这个,您可以将(例如:我们50个结果)保存到文件中。

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')
#######################