用Python进行Google搜索

7

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


1
假设您真正想知道如何做到这一点,那么您至少有两个单独的问题:1. 如何使用Python“在Google上搜索特定输入”(我假设您的意思是“搜索”,而不是“搜索”。即:“特定输入”是查询,而不是语料库,对吗?)2. 如何将信息存储在“doc”文件中(Microsoft Word .doc文件或其他文件?)。 - Laurence Gonsalves
2
请注意,对于将来,请注意应该将这些问题分别作为两个独立的问题提出,因为它们根本没有太多相关性。 - moinudin
3个回答

16

使用提供的API。首先注册以获取API密钥这里。然后,您可以使用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文档,请阅读此问题

2
这个答案很危险。它会在某些(罕见的)响应上崩溃,并且它包含一个远程代码执行安全漏洞。(marcog:我知道你已经在那里放置了“谨防eval()不受信任的数据!”的评论。但是有很多人会忽略这个警告,因为“它可以工作,为什么要改变它”。然后他们将编写危险的代码)。另请参阅https://dev59.com/N3NA5IYBdhLWcg3wH6EW - user9876
2
为什么要使用eval(),而不是更安全的json.load()呢? - Aphex
2
@user9876 @Aphex 在回答中进行了更改。我对Python中的JSON处理不是很熟悉,所以感谢您指出这一点。 - moinudin

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获取URL,使用simplejson解码它。如果您没有这些软件包,请在Google上搜索它们。您可以使用json.load()将响应转换为Python字典,您可以轻松读取。祝你愉快!
编辑:至于创建Word文档,您有多种选择,详见此处:如何使用Python创建Word文档?

0

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

第一 - 您想使用Python语言在Google上执行搜索查询。

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

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

如果您想要使用键盘快捷键宏制作计算机动作脚本。

您可以尝试在Windows系统上使用AutoPythonLauncher软件。有关更多信息请点击此处 或查看此Youtube视频-您可以看到它能做什么。

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

1-回答第一个问题:

使用此代码,您可以选择任何文本并使用查询参数(例如:us、50个结果)直接进行GOOGLE搜索

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

第二个问题的答案:

选择一张图片并在命令编辑器中编写此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')
#######################

哦,你使用pywinauto启动应用程序,但仅此而已。对于这种简单情况,subprocess.Popen("notepad.exe")就足够了。但是pywinauto有更强大的功能,可以自动等待打开窗口。因此,您不必添加硬编码的time.sleep调用。不知道为什么您只使用Python的1%。 - Vasily Ryabov
@ Vasily Ryabov - 1 - pywinauto包可以做很多比仅仅启动应用程序更多的事情,这就是我选择这个命令的原因。2 - 我现在知道你可以将time.sleep(2)替换为pywinauto.Application().WindowSpecification.Wait('enabled')(等待存在记事本窗口)-但仍然不起作用,我尝试过,计算机需要更多时间来存在,然后激活记事本窗口-对于初学者来说,解释命令time.sleep(2)很简单-用户可以随时简单地更改睡眠时间以加快宏移动速度。 - stevecody
更详细地解释不起作用的情况可能会更好。我们最近对.wait方法进行了一些修复(在0.6.3中)。所以我很惊讶它仍然没有起作用。 - Vasily Ryabov
此外,.wait(...)time.sleep 总是更快,因为超时只是等待的最长时间。关于 exists + enabled,我们计划在明年夏季的大版本中重新设计它。因此,查找元素和检查其状态将是分开的。 - Vasily Ryabov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接