如何使用Python在Google上执行搜索查询?如何将搜索结果存储在Microsoft Word文档中?
如何使用Python在Google上执行搜索查询?如何将搜索结果存储在Microsoft Word文档中?
使用提供的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
。eval()
,而不是更安全的json.load()
呢? - Aphexhttp://code.google.com/apis/customsearch/v1/getting_started.html
http://code.google.com/apis/customsearch/v1/using_rest.html
Google的自定义搜索API似乎是您要寻找的。您需要先获取API密钥,然后似乎可以进行最多100次搜索。一个问题的价格是两个问题:
第一 - 您想使用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')
#######################
subprocess.Popen("notepad.exe")
就足够了。但是pywinauto有更强大的功能,可以自动等待打开窗口。因此,您不必添加硬编码的time.sleep
调用。不知道为什么您只使用Python的1%。 - Vasily Ryabov.wait
方法进行了一些修复(在0.6.3中)。所以我很惊讶它仍然没有起作用。 - Vasily Ryabov.wait(...)
比 time.sleep
总是更快,因为超时只是等待的最长时间。关于 exists
+ enabled
,我们计划在明年夏季的大版本中重新设计它。因此,查找元素和检查其状态将是分开的。 - Vasily Ryabov