【问题标题】:webbrowser.open() breaks the link (python 3.7)webbrowser.open() 断开链接(python 3.7)
【发布时间】:2018-07-01 13:03:04
【问题描述】:

当我跑步时

import webbrowser
webbrowser.open('https://google.com')

在 Python 3.7 中,它会在我的默认浏览器中打开损坏的 url:http://openurl%28https//google.com,new-window)。当我在 3.6 中运行相同的代码时,它可以正常工作。这是 Python 3.7 中的错误还是我配置错误?

我正在使用:

  • Linux Manjaro
  • Python 3.7.0 安装了 pyenv (env PYTHON_CONFIGURE_OPTS="--enable-shared --enable-optimizations" pyenv install 3.7.0)
  • Opera 是我的默认浏览器
  • webbrowser.get() 返回<webbrowser.Opera object at 0x7f546c22ea90>
  • 我的os.environ dump

【问题讨论】:

  • 您使用的是哪个浏览器?查看源代码,您的 Python3.7 似乎尝试使用......Netscape 或 Opera 来打开 url? github.com/python/cpython/blob/master/Lib/webbrowser.py#L281
  • 无法在 Win 7 上的 Python 3.7 上重现
  • 另外,os.environ 的输出是什么?
  • 我在主帖中添加了所有这些信息。
  • 此问题在下一个 Python 次要版本中修复!

标签: python python-3.x python-3.7


【解决方案1】:

编辑This issue 在 Python 端是固定的。在 Python 的下一个次要版本之后,您不需要考虑这个问题。不过,下面的答案仍然是有用的分析。


在 Python3.6 中,webbrowser.get() 会给你类似的东西:

<webbrowser.BackgroundBrowser object at 0x7f1e5373a048>

但在 Python3.7 中:

<webbrowser.Opera object at 0x7f546c22ea90>.

这意味着在 Python3.6 中它将尝试使用 XDG 标准打开 URL:

xdg-open https://google.com

而在 Python3.7 中它会直接使用 its CLI command 打开 Opera 浏览器:

opera -remote "openURL(https://google.com,new-window)"

虽然我不确定此更改是否有意,但两种方式都应该是正确的。问题是,Opera 的命令行选项已损坏。这不是 Python 的错,而是 Opera 的错误。 (尝试使用上面的命令打开,你会看到相同的http://openurl%28https//google.com,new-window)。)

对于解决方法,您可以通过 XDG 标准强制打开 URL,使用 register() with the new preferred argument introduced in 3.7

import webbrowser
webbrowser.register("xdg-open", None, webbrowser.BackgroundBrowser("xdg-open"), preferred=True)
print(webbrowser.get())  # Now you will see <webbrowser.BackgroundBrowser object at 0x7f1e5373a048>
webbrowser.open('https://google.com')

希望对你有帮助!

编辑:看起来 Opera 的错误是因为他们将引擎更改为 Chromium 后没有更新文档。 Opera 与 Chrom 的 CLI 参数配合得很好。我issued a change to Python 反映了这一点。

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 2014-04-22
    • 2011-02-07
    • 1970-01-01
    • 2014-03-27
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多