【问题标题】:"ImportError: cannot import name Browser" with python mechanize带有python mechanize的“ImportError:无法导入名称浏览器”
【发布时间】:2017-02-16 10:02:39
【问题描述】:

我无法使用 mechanize code )。导致错误的部分是

#!/usr/bin/python
import re
from mechanize import 
br = Browser()

我也试过了

用python3.5执行时,发现如下错误:

# python mechanize.py
Traceback (most recent call last):
  File "mechanize.py", line 6, in <module>
    from mechanize import Browser
  File "/root/git/stakexchange-ask-question/mechanize.py", line 6, in <module>
    from mechanize import Browser
ImportError: cannot import name Browser

不过这正是官方mechanize website所建议的

如果我将代码修改为

#!/usr/bin/python
import re
br = mechanize.Browser()

我也遇到了错误

   # python  mechanize.py
Traceback (most recent call last):
  File "mechanize.py", line 5, in <module>
    import mechanize
  File "/root/git/stakexchange-ask-question/mechanize.py", line 6, in <module>
    br =mechanize.Browser()
AttributeError: module 'mechanize' has no attribute 'Browser'

我已经用

安装了mechanize
easy_install mechanize

【问题讨论】:

    标签: python html linux forms mechanize


    【解决方案1】:

    你试过 mechanize.Browser() 吗?

    【讨论】:

    • http://wwwsearch.sourceforge.net/mechanize/ 显示不需要from mechanize import Browser 执行mechanize.Browser() 但如果删除from mechanize import Browser 的代码,我得到AttributeError: 'module' object has no attribute 'Browser'
    • 我的回答太快了。确保您不在 Python wwwsearch.sourceforge.net/mechanize/faq.html
    • 你是对的。我的是python 2.7。非常感谢。
    • 实际上在这种情况下它应该可以工作。在我想说你必须有一个低于 3. 的 python 版本之前,这个版本还不支持机械化它。
    【解决方案2】:

    python 版本必须至少为 3.0 (reference)

    检查你的python版本

    readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V'
    

    但错误不仅仅来自于此。 mechanize 未正确安装,我从 source code 重新安装。

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2014-09-27
      • 2020-04-09
      • 2016-12-24
      • 2012-11-12
      • 1970-01-01
      相关资源
      最近更新 更多