我有一个小脚本(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/