使用 Python 进行谷歌搜索

发布于 2024-10-08 11:14:54 字数 66 浏览 5 评论 0原文

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

How do you perform a search query on Google using Python? How do you store the search results in a Microsoft Word document?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木格 2024-10-15 11:14:54

使用提供的 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

Whichoutputs

{   '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 文档,请阅读此问题。

Use the provided API. First register to get an API key here. Then you can use Python's urllib2 package to fetch the results, e.g.

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

Which outputs

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

Please make sure to replace YOUR_KEY_HERE with your key.

To create an MS Word document from Python, read this question.

满天都是小星星 2024-10-15 11:14:54

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 文档?

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

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

Google's custom search API looks to be what you're looking for. You'll need to get a API key first; then it seems they let you do up to 100 searches a day.

Use urllib2 to fetch the URL, and simplejson to decode it. (Google around for these packages if you don't already have them.) You can use json.load() to turn the response into a python dictionary that you can easily read from. Happy hacking!

Edit: As for creating the word document, you have a variety of options, detailed here: How can I create a Word document using Python?

停滞 2024-10-15 11:14:54

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

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

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

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

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

您可以尝试在 Windows 系统上使用 AutoPythonLauncher 软件。欲了解更多信息点击此处
或者观看这个 Youtube 视频 - 您可以看到它的功能。

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

1 - 回答第一个问题:

使用此代码,您可以选择任何文本并使用查询参数直接进行 GOOGLE 搜索(例如:us,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')
#######################

One Question for the price of two Questions:

First - You Want to perform a search query on Google using with Python Languages.

Second - You want to save the search results into a Microsoft Word document.

Hi, I like Autohotkey Tools with Python Languages.

If you like to, make Computer Movements Scripts With Keyboard Shortcuts Macros.

You can try to use AutoPythonLauncher Software on your Windows System. For more info Click Here
or look to this Youtube Video - you can see what it can do.

With this Tool you can Make (Toolbars) - and Create a Set of Clickable Pictures with Python Command Scripts. (Keyboard Shortcuts Combinations)

1 - Answer the First Question:

With this code, You can Select any text and do direct a GOOGLE SEARCH with query parameters (example:us,50 results)

With AutoPythonLauncher you can Choose a Picture and Write in the Command Editor this Python Code.

Save and Restart AutoPythonLauncher and you can use it as a LAUNCHER.
Select any text and Click With your MOUSE or TOUCH device on that Picture, and it is Done.

# 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 - Answer the Second Question:

Choose a Picture and Write in the Command Editor this Python Code.
With this you can then save the (example:us 50 results) to a file.

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')
#######################
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文