python - BeautifulSoup已安装,但miniconda仍然抛出属性错误

标签 python beautifulsoup python-3.4 anaconda

我有一个小脚本(bot.py)来抓取链接,它使用 BeautifulSoup 和 urllib (python 3.4)。

import urllib
import urllib.request as url_req
from bs4 import BeautifulSoup
import re


url = input('URL: ')
header = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

req = url_req.Request(url, headers=header)
try:
    response = url_req.urlopen(req)
except urllib.error.URLError as e:
    print(e.reason)

html = response.read()
soup = BeautifulSoup.BeautifulSoup(html)

for link in soup.findAll('a',   attrs={'href': re.compile("^http://")}):
    #print(link)
    print(link.get('href'))

当我在 Miniconda 中运行它时,出现以下错误:

Traceback (most recent call last):
  File "StanBot.py", line 17, in <module>
    soup = BeautifulSoup.BeautifulSoup(html)
AttributeError: type object 'BeautifulSoup' has no attribute 'BeautifulSoup'

但是BeautifulSoup已经安装在miniconda中:

[py34] C:\Users\Anna\Desktop>conda list
# packages in environment at C:\Miniconda3\envs\py34:
#
beautiful-soup            4.3.2                    py34_1
beautifulsoup4            4.3.2                     <pip>
msvc_runtime              1.0.1                    vc10_0  [vc10]
pip                       7.1.2                    py34_0
python                    3.4.3                         4
setuptools                18.5                     py34_0
wheel                     0.26.0                   py34_1

[py34] C:\Users\Anna\Desktop>

我进行了很多搜索,但无法找到合适的解决方案。这是什么原因造成的?

最佳答案

尝试使用soup = BeautifulSoup(html)

关于python - BeautifulSoup已安装,但miniconda仍然抛出属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980334/

相关文章:

python - 如何读写大于主内存的numpy数组?

python - 如何加快 aiohttp 解析器 bs4 的速度?

python - 使用 py2exe 和 BeautifulSoup。脚本运行正常,但转换为 .exe 后,显示错误

python - 使用 Python 匹配文件中的数字

mongodb - pymongo 无法检索文件

python-requests - 无法使用 Python3 模块请求 POST 到 Grafana

python - 数据库不会使用 MySQL 和 Python 自动更新

python - pyautogui Python 3.6 与 ok 按钮交互

python - 列表列表中的非规范化层次结构

python - 使用 BeautifulSoup 在 Python 中提取嵌入式 <span>