Kojimosa
Kojimosa

Reputation: 31

opening google searches with python

I want to make my program open first 5 searches of google. Currently I have only simple google search generator which opens a search. Is there a way to open 5 searches into new tabs then close them ?

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)

EDIT:

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

to open new tab, but now could anyone tell me how to click first 5 results which I get from opening some google search EDIT so I figured a way out with lib called google

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

also I just decided that I would close them all just with task kill because webbroser lib doesn't have close window command

Upvotes: 3

Views: 17569

Answers (1)

user812786
user812786

Reputation: 4430

Use the open_new_tab function:

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)

This should open your URLs each in a new tab, if supported by the browser. If not, it will fall back to opening a new window for each tab.

If you have issues getting it to open specifically in Chrome, even as default browser (see here), try replacing the last line with:

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

using the equivalent location of your Chrome install.

Upvotes: 7

Related Questions