我试图废弃一个网站,但BeautifulSoup正在给我一个错误。我不确定导致错误Bs4和html5lib的安装是什么。有没有人有这个想法?
Python代码
from bs4 import BeautifulSoup
import requests
url = 'http://www.transtats.bts.gov/Data_Elements.aspx?Data=1r'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html,"html5lib")
print (soup.prettify())
Python错误
runfile('C:/WebsiteGrab.py', wdir=;'somepath')
Traceback (most recent call last):
File "<ipython-input-1-fc28ecb678ac>", line 1, in <module>
runfile('C:/Users/bartogre/Desktop/WebsiteGrab.py', wdir='C:/Users/bartogre/Desktop')
File "C:\Program Files (x86)\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files (x86)\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/bartogre/Desktop/WebsiteGrab.py", line 12, in <module>
soup = BeautifulSoup(html,"html5lib")
File "C:\Program Files (x86)\Anaconda3\lib\site-packages\bs4\__init__.py", line 165, in __init__
% ",".join(features))
FeatureNotFound: Couldn't find a tree builder with the features you requested: html5lib. Do you need to install a parser library?
答案 0 :(得分:0)
根据&#39; FeatureNotFound&#39;消息,尝试删除/删除与C:\ Python(版本)\ Lib \ site-packages \中的html5lib相关联的所有文件夹(libriries)(不要删除bs4文件夹中的任何内容)。您将在安装html5lib后测试问题是否正确。
不使用html5lib进行测试:
from bs4 import BeautifulSoup
import urllib.request
url = "https://www.crummy.com/software/BeautifulSoup/bs4/doc/"
response = urllib.request.urlopen(url)
soup = BeautifulSoup(response)
print (soup.prettify())
如果上面的代码有效,那么问题在于安装html5lib解析器。如果bs4函数不适合你,试试一个新的解析器。