用python打开google搜索

2024-06-10 09:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让我的程序打开谷歌的前5个搜索。目前我只有一个简单的谷歌搜索生成器,打开一个搜索。有没有办法在新的标签页中打开5个搜索,然后关闭它们?

dict = readdict()

lenght = len(dict)
search_term=""
for _ in range(self.variable2.get()):
    skc=randint(0,lenght)

    word = dict[skc].split(',')

    search_term+=word[0]+" "

url = "https://www.google.com.tr/search?q={}".format(search_term)    
webbrowser.open(url)

编辑:

url = "https://www.google.com.tr/search?q={}".format(term)
webbrowser.open_new_tab(url)

打开新的标签,但现在有谁能告诉我如何点击前5个结果,我从打开一些谷歌搜索 编辑 所以我想出了一个用lib的方法叫google

for url in search('"search_term', stop=5):

另外,我决定只使用task kill关闭它们,因为webbroser lib没有close window命令


Tags: inhttpscomurlforsearchwwwgoogle
4条回答

使用^{}函数:

import webbrowser
search_terms = []

# ... construct your list of search terms ...

for term in search_terms:
    url = "https://www.google.com.tr/search?q={}".format(term)
    webbrowser.open_new_tab(url)

如果浏览器支持,这将在新选项卡中打开每个URL。如果没有,则返回到为每个选项卡打开一个新窗口。

如果您无法在Chrome中特别打开它,即使是作为默认浏览器(see here),请尝试将最后一行替换为:

chrome_browser = webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s")
chrome_browser.open_new_tab(url)

使用Chrome安装的等效位置。

相关问题 更多 >