3

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

Kojimosa
  • 31
  • 1
  • 1
  • 3
  • Why is this tagged with google-crome? – linusg Jul 19 '16 at 13:43
  • @linusg, I'm guessing OP has Chrome as their default browser. – user812786 Jul 19 '16 at 15:44
  • @whrrgarbl - Yes, that might be a reason. I just saw your answer and remembered me using the `open_new_tab` function - which opened new *Windows* instead of *Tabs* using Firefox, IDK why. I've tried the code snippet, the default Browser of my OS is Firefox, and **it opens new windows of InternetExplorer - Arhhhh!** – linusg Jul 19 '16 at 15:53
  • I changed the default browser to Chrome, Edge, FF - nothing. It always opens the IE in new windows. – linusg Jul 19 '16 at 15:57
  • @linusg uh oh. I thought it was just my (restricted) work setup doing that... I'll try a few things. Looks like this is a common issue though: http://stackoverflow.com/questions/22445217/python-webbrowser-open-to-open-chrome-browser – user812786 Jul 19 '16 at 16:00
  • @whrrgarbl - Strange, why do the implement it like this? Ok, IE is installed on every Windows PC, and on a fresh Windows installation, there's no default browser set AFAIK, but what opens on a Linux machine? IE is not given there, and sometimes no browser is installed. I think I'll have a look at the sources now. – linusg Jul 19 '16 at 16:05
  • Yes im using chrome as default but in future I would use chrome_browser = webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s") to have multiple support – Kojimosa Jul 20 '16 at 05:26

1 Answers1

7

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.

Community
  • 1
  • 1
user812786
  • 4,302
  • 5
  • 38
  • 50
  • okay that solves one problem, but how could i open first 5 search results? – Kojimosa Jul 20 '16 at 05:21
  • @Kojimosa Oh, when I posted I thought your main issue was with the new tabs and you already had URLs. I don't know offhand what sort of API Google offers, but a quick search turned up http://stackoverflow.com/questions/1657570/google-search-from-a-python-app, http://stackoverflow.com/questions/3898574/google-search-using-python-script, and http://stackoverflow.com/questions/35125696/search-all-of-google-with-google-python-api which may be useful. I see your question edits though, have you solved that part already? – user812786 Jul 20 '16 at 11:35
  • yes I solved practicly most part with google module, just didn't found right way to close tabs – Kojimosa Jul 21 '16 at 06:17
  • You can't close specific tabs through webbrowser, see http://stackoverflow.com/questions/30326196/how-to-close-the-existing-browser-tab-using-the-python-webbrowser-package. Unfortunately it seems you're pretty much limited to issuing an OS command to kill all Chrome processes. – user812786 Jul 21 '16 at 13:34