Reputation: 55
So what i want to do is very simple. I have a list of words and for each word in the list i want to open three websites. Like website 1: word 1,website 2: word 1,website 3: word 1,website 1: word 2, ... . My attempt to do so was as fallow:
import webbrowser
wordList = ["word1", "word2", "word3",]
for word in wordList:
url = "https://www.ldoceonline.com/dictionary/" + word
webbrowser.open(url, new=2, autoraise=False)
url = "https://dictionary.cambridge.org/dictionary/english/" + word
webbrowser.open(url, new=2, autoraise=False)
url = "https://www.google.com/search?q=" + word
webbrowser.open(url, new=2, autoraise=False)
The problem is when the list is long then the websites do not open in order. Is there a way to make sure they open in correct order?
I use Firefox as default browser and my OS is windows.
Upvotes: 0
Views: 816
Reputation: 1365
It seems that the URLs are being opened too fast. If the main focus is that they should be in order, then I would suggest the following code:
import webbrowser
import time
LOAD_TIME = 0.2 # s
word_list = ["word1", "word2", "word3",]
url_list = ["https://www.ldoceonline.com/dictionary/", "https://dictionary.cambridge.org/dictionary/english/", "https://www.google.com/search?q="]
for word, url in zip(word_list, url_list):
status = webbrowser.open(url+word, new=2, autoraise=False)
time.sleep(LOAD_TIME)
It just gives the script some time before moving on to the next open()
exicution.
The looping through the combined variables is set up with a cleaner zip
, and you can test the variable LOAD_TIME
a few times. You might need a longer or shorter load time interval depending on your machine, internet speed, etc. If speed is not a concern but the order of the tabs is most important, you can set this variable to be quite high.
EDIT: If you want to avoid the time
module, the following also worked for me:
import webbrowser
word_list = ["word1", "word2", "word3",]
url_list = ["https://www.ldoceonline.com/dictionary/", "https://dictionary.cambridge.org/dictionary/english/", "https://www.google.com/search?q="]
for word, url in zip(word_list, url_list):
if webbrowser.open(url+word, new=2, autoraise=False):
continue
Explanation: The code waits for a response of True
or False
from the current webbrowser.open()
call before moving on to the next.
EDIT2:
If you want to open each word in all three links, then the zip
function is not the one. You might want a double-for loop instead. If you want the order in which the links are opened to be (url1+word1), (url2+word1), (url3+word1), (url1+word2), ...
, then the double for loop would look like:
import webbrowser
word_list = ["word1", "word2", "word3",]
url_list = ["https://www.ldoceonline.com/dictionary/", "https://dictionary.cambridge.org/dictionary/english/", "https://www.google.com/search?q="]
for word in word_list:
for url in url_list:
if webbrowser.open(url+word, new=2, autoraise=False):
continue
Upvotes: 1