1
我有一個腳本(bot.py)來刮擦鏈接,它使用BeautifulSoup urllib(python 3.4)。BeautifulSoup安裝,仍然miniconda投擲屬性錯誤
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>
我搜索了很多,但couldn」提出一個適當的解決方案。什麼導致這個?
嘗試用'BeautifulSoup(html)替換'BeautifulSoup.BeautifulSoup(html)'' –