我有一个小脚本(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>
我搜索了很多,但无法找到合适的解决方案。是什么造成的?
答案 0 :(得分:1)
请尝试soup = BeautifulSoup(html)
。