使用Python进行Google搜索
你想知道怎么用Python在谷歌上搜索东西吗?还有,怎么把搜索到的结果存到一个Word文档里?
3 个回答
一个问题换两个问题:
首先 - 你想用Python语言在谷歌上进行搜索。
其次 - 你想把搜索结果保存到一个Microsoft Word文档里。
你好,我喜欢用Python语言的Autohotkey工具。
如果你也喜欢,可以用键盘快捷键制作电脑操作脚本。
你可以试试在Windows系统上使用AutoPythonLauncher软件。想了解更多信息,可以点击这里,或者看看这个Youtube视频 - 你可以看到它能做什么。
通过这个工具,你可以制作工具栏,并创建一组可点击的图片,使用Python命令脚本(键盘快捷键组合)。
1 - 回答第一个问题:
使用这段代码,你可以选择任何文本,并直接进行谷歌搜索,带上查询参数(例如:us,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代码。
这样你就可以把(例如: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')
#######################
http://code.google.com/apis/customsearch/v1/getting_started.html
http://code.google.com/apis/customsearch/v1/using_rest.html
谷歌的自定义搜索API看起来正是你需要的东西。首先,你需要获取一个API密钥;然后,他们似乎允许你每天进行最多100次搜索。
你可以使用 urllib2
来获取网址,然后用 simplejson
来解码。如果你还没有这些包,可以在网上搜索一下。你可以用 json.load()
把响应转换成一个Python字典,这样你就能轻松读取了。祝你编程愉快!
编辑:关于创建Word文档,你有很多选择,详细信息可以在这里找到: 如何使用Python创建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
替换成你自己的密钥哦。
如果你想从Python创建一个MS Word文档,可以参考这个问题。