简体   繁体   English

使用 webbrowser.open 按顺序打开多个网页

[英]Opening multiple web page in order using webbrowser.open

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, ... .像网站 1:单词 1,网站 2:单词 1,网站 3:单词 1,网站 1:单词 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.我使用 Firefox 作为默认浏览器,我的操作系统是 windows。

It seems that the URLs are being opened too fast.似乎 URL 的打开速度太快了。 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.它只是在继续下一个open()执行之前给脚本一些时间。

The looping through the combined variables is set up with a cleaner zip , and you can test the variable LOAD_TIME a few times.组合变量的循环使用更清晰的zip设置,您可以多次测试变量LOAD_TIME 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:编辑:如果您想避免使用time模块,以下内容也对我有用:

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.说明:代码等待来自当前webbrowser.open()调用的TrueFalse响应,然后再继续下一个。

EDIT2: If you want to open each word in all three links, then the zip function is not the one. EDIT2:如果您想打开所有三个链接中的每个单词,那么zip功能不是一个。 You might want a double-for loop instead.您可能需要一个双 for 循环。 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:如果您希望打开链接的顺序为(url1+word1), (url2+word1), (url3+word1), (url1+word2), ... ,那么双 for 循环将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM