机械化浏览器没有属性

mechanize browser has no attribute

我已经重写了两次,我看不出问题所在,我在 python 2 上安装了 mechanize,我没有同时使用制表符和空格,我只是在做空间。

import urllib
from bs4 import BeautifulSoup
import re
import urlparse
import mechanize

url = "http://www.dailymail.co.uk/home/index.html"
br = mechanize.Browser()
br.open(url)

for link in br.links():
    print link

错误是

Traceback (most recent call last):
  File "mechanize.py", line 4, in <module>
    import mechanize
  File "/home/ro/Autoblog/mechanize/mechanize.py", line 8, in <module>
    br = mechanize.Browser()
AttributeError: 'module' object has no attribute 'Browser'

您的 Python 文件的文件名是 "mechanize.py" (/home/ro/Autoblog/mechanize/mechanize.py)。这隐藏了 "mechanize" 模块的名称。由于这种名称空间冲突,您实际上是在导入自己的脚本:import mechanize.

将您的 "mechanize.py" 脚本的名称更改为其他名称,您应该会很好。

基本上你有一个冲突文件,它来自你下载 mechanize 的 github,每个 mechanize 文件夹中有两个 _mechanize.py 所以,删除第二个 mechanize 文件夹mechanize 文件夹内的 mechanize 文件夹