用python打开谷歌搜索

时间:2016-07-19 13:22:55

标签: python google-chrome

我想让我的程序打开谷歌的前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个结果 的修改 所以我想出了一个名为google

的lib出路
for url in search('"search_term', stop=5):

我还决定只关闭任务就关闭它们因为webbroser lib没有关闭窗口命令

1 个答案:

答案 0 :(得分:2)

使用open_new_tab功能:

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)

如果浏览器支持,则应在新标签页中打开您的网址。如果没有,它将回退到为每个标签打开一个新窗口。

如果您在Chrome中专门打开它时遇到问题,即使是默认浏览器(see here),请尝试将最后一行替换为:

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

使用Chrome安装的等效位置。