2010-12-20 43 views
7

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

+1

假设你想真正知道如何做到这一点,你有至少两个单独的问题在这里:你是怎样“搜索某个输入对谷歌使用Python”(我假设你的意思是“搜索为“而不是”搜索“,即:”某些输入“是查询,而不是语料库,对不对?)。 2.如何将信息存储在“doc”文件(Microsoft Word .doc文件或其他内容?)中。 – 2010-12-20 15:29:13

+2

未来,请注意,您应该将这些问题作为两个单独的问题提出,因为它们根本不相关。 – marcog 2010-12-20 15:42:56

回答

3

看到这个问题

Google Search from a Python App

包含从亚历马尔泰利的答案(蟒蛇2.6)和Python 3的端口也是如此。 您应该可以相应地修改它。 它使用JSON和urllib的是@Aphex提到了两个问题的价格

+2

在我看来,那里的答案使用现在不推荐使用的网络搜索API:这里的其他答案使用当前的自定义搜索API。 – 2010-12-20 16:10:29

+0

@JohnY - 是的,但[自定义搜索API](http://code.google.com/apis/customsearch/v1/getting_started.html)要求您配置要搜索的网站 - 您如何设置它直到搜索整个网络,而不仅仅是一个子集? – Day 2011-12-27 16:48:58

+0

@Day:我不知道。我没有用过它。上次我读到任何有关它的信息时,似乎并不像应该那样容易。我认为谷歌试图阻止人们使用他们的全网搜索,但我也不知道其原因。 – 2011-12-28 02:13:46

3

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

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

谷歌的自定义搜索API看起来是你在找什么。您需要先获取API密钥;那么他们似乎可以让你每天进行100次搜索。

使用urllib2来获取URL和simplejson来解码它。 (如果你还没有这些软件包的话,你可以使用Google的这些软件包。)你可以使用json.load()来将响应转换成一个可以轻松阅读的Python字典。快乐黑客!

编辑:作为创建Word文档,你有多种选择,这里详细:How can I create a Word document using Python?

15

使用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

+2

这个回答很危险。它会打破某些(罕见的)响应,并且它包含远程代码执行安全漏洞。 (marcog:我知道你已经把“Beware eval()不受信任的数据!”)评论了,但是有很多人会忽略这个警告,因为“它起作用了,为什么我应该改变它”然后他们会写危险代码)。另请参阅http://stackoverflow.com/questions/1083250/running-json-through-pythons-eval – user9876 2010-12-20 15:49:28

+2

是的,为什么在使用更安全的'json.load()'时使用'eval()'? – Aphex 2010-12-20 15:51:27

+2

@ user9876 @Aphex改变了答案。我对Python中的json处理不是很熟悉,所以谢谢指出。 – marcog 2010-12-20 15:59:39

0

一个问题:

首先 - 要执行的Python语言使用上谷歌搜索查询。

秒 - 您想要将搜索结果保存到Microsoft Word文档中。

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

如果你喜欢,使用键盘快捷键宏制作计算机移动脚本。

您可以尝试在Windows系统上使用AutoPythonLauncher软件。欲了解更多信息Click Here 或期待这Youtube Video - 你可以看到它可以做什么。

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

1 - 回答第一个问题:

有了这个代码,您可以选择任何文字和做直接与查询参数谷歌搜索(例如:美国,50个结果)

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

保存并重新启动AutoPythonLauncher,您可以将它用作LAUNCHER。 选择任意文字并在该图片上单击鼠标或触控设备,即可完成。

# 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代码编写。 使用此功能,您可以将(例如:us 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') 
####################### 
+0

哦,你使用pywinauto来启动应用程序,但没有更多。对于这种简单的情况'subprocess.Popen(“notepad.exe”)就足够了。但pywinauto有更强大的功能,自动等待打开窗口。所以你不必添加硬编码的'time.sleep'调用。不知道为什么你只使用1%的Python功能。 – 2017-11-30 03:34:34

+0

@ Vasily Ryabov - 1 - pywinauto包你可以做更多的事情,然后才启动应用程序,这就是为什么我选择这个命令的原因。 2 - 我现在可以将time.sleep(2)替换为** pywinauto.Application()。WindowSpecification.Wait('enabled')**(等到** Exist **记事本窗口) - 但仍然没有我尝试过的工作,计算机需要多一点时间才能存在**然后**激活**记事本窗口 - 对于初学者来说,解释命令时间很简单。睡眠(2) - 用户可以随时简单地改变睡眠时间,以加快宏观运动。 – stevecody 2017-12-01 12:31:02

+0

在更详细的情况下解释非工作场景可能很好。我们最近修正了'.wait'方法(在0.6.3中)。所以我很惊讶它仍然没有工作。 – 2017-12-02 07:42:29