我正在使用网络浏览器模块编写Python脚本,以自动打开所需的网页
我面临的问题是,我只能在不同的浏览器窗口上打开网页,而不能在不同的选项卡上打开同一个浏览器窗口。
下面是我正在使用的代码。
#! /usr/bin/python -tt
import webbrowser
def main():
webbrowser.open('url1')
webbrowser.open('url2')
webbrowser.open('url3')
if __name__ == '__main__':
main()
我想在同一个web浏览器窗口的不同选项卡上打开所有这些链接,而不是在不同的浏览器窗口上。感谢:)
您需要使用webbrowser.open_new_tab(url)
。例如
import webbrowser
url = 'http://www.stackoverflow.com'
url2 = 'http://www.stackexchange.com'
def main():
webbrowser.open(url2) # To open new window
print('Opening Stack Exchange website!')
webbrowser.open_new_tab(url) # To open in new tab
print('Opening Stack Overflow website in a new tab!')
if __name__ == '__main__':
main()
在python 3.6中,完整的答案将包括Web浏览器文档中的webbrowser.open_new()
和webbrowser.open_new_tab()
。
import webbrowser
def main():
# print(webbrowser._browsers) # for Python 3.x to determine .get() arg
browser = webbrowser.get('firefox')
urls = ['url1', 'url2', 'url3']
first = True
for url in urls:
if first:
browser.open_new(url)
first = False
else:
browser.open_new_tab(url)
if __name__ == '__main__':
main()
享受代码+1如果它能帮你的话。干杯
简单的webbrowser.open_new_tab('url')