1

我正在尝试以下操作:

import webbrowser
url = 'http://docs.python.org/'
webbrowser.open_new_tab(url)

结果是一个空的新窗口(在地址行中没有输入 url)。浏览器是在 Ubuntu 16.04 LTS 下运行的 Chrome(版本 53.0.2785.92,64 位)。Python 的版本是 3.5.2。

我怎样才能解决这个问题?

4

2 回答 2

0

尝试在您的浏览器窗口打开的情况下执行以下操作(更新:也尝试在没有打开浏览器窗口并且浏览器设置设置为上次打开我的窗口和选项卡的情况下进行。这两项试验都使用 Chrome 和 Firefox 进行了测试)

import webbrowser
webbrowser.open("http://docs.python.org", new=2)

从文档

webbrowser.open(url, new=0, autoraise=True)

使用默认浏览器显示 url。如果 new 为 0,则尽可能在同一浏览器窗口中打开 url。如果 new 为 1,则尽可能打开一个新的浏览器窗口。如果 new 为 2,则尽可能打开一个新的浏览器页面(“选项卡”)。如果 autoraise 为 True,则尽可能提升窗口(请注意,在许多窗口管理器下,无论此变量的设置如何,都会发生这种情况)。

Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
于 2016-12-27T23:10:23.693 回答
0

First, I tried:

>>> webbrowser.get('chrome')

This did not work. The reason is that the executable for Chrome is /usr/bin/google-chrome! So, I went to /usr/bin and issued the following command in the terminal:

sudo ln -s google-chrome chrome

Now, this works:

>>> webbrowser.get('chrome').open_new_tab('http://www.python.org')

P.S. I am still wondering how to get webbrowser.get('') to work. The default browser is set to be Google-Chrome-Stable...

于 2016-12-28T20:33:26.553 回答