【hacker的错误集】html5lib使用报错Couldn‘t find a tree builder with the features you requested: html5lib
错误 报错 The with Find Tree you Builder
2023-09-11 14:19:16 时间
✅作者简介:大家好我是hacker707,大家可以叫我hacker
📃个人主页:hacker707的csdn博客
🔥系列专栏:hacker的错误集
💬推荐一款模拟面试、刷题神器👉点击跳转进入网站
报错内容
报错代码如下:
import requests
from bs4 import BeautifulSoup
import csv
class WeatherData():
def __init__(self):
self.header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}
self.head = ['city_name', 'city_temp']
self.data_list = []
def get_html(self, url):
# 得到响应结果
response_obj = requests.get(url, headers=self.header)
# 从响应结果中获取源码
html = response_obj.content.decode('utf-8')
return html
def parse_data(self, html):
# 我们发现港澳台网页的数据 出现标签不全的情况 影响数据的爬取
# 所以采用'html5lib'能够实现自动补全 缺点:速度比较慢
soup = BeautifulSoup(html, 'html5lib')
# 2.1 我们先找到整页的数据 class="conMidtab"的div标签中
conMidtab = soup.find(class_="conMidtab")
# 2.2 再去找每一个省或者是直辖市所有对应的table标签
table_list = conMidtab.find_all('table')
for table in table_list:
# 2.3 再去table标签里面找tr标签 每一个tr标签存放的是一个城市的数据 需要把前两个tr标签过滤(表头)
tr_list = table.find_all('tr')[2:]
for index, tr in enumerate(tr_list):
item = {}
# 2.4 最后去tr中找td标签 第零个td标签是城市名字 倒数第二个td标签是最低温
if index == 0:
city_td = tr.find_all('td')[1]
else:
city_td = tr.find_all('td')[0]
# print(td.string) # None
# city_name 去第零个td标签中获取城市名字
city_name = list(city_td.stripped_strings)[0]
# city_temp 倒数第二个td标签是最低温
temp_td = tr.find_all('td')[-2]
city_temp = list(temp_td.stripped_strings)[0]
# print(city_name, city_temp)
item['city_name'] = city_name
item['city_temp'] = city_temp
# print(item)
self.data_list.append(item)
def save_data(self):
'''
保存数据
'''
with open('weather.csv', 'w', encoding='utf-8-sig', newline='') as file_obj:
DictWriter = csv.DictWriter(file_obj, self.head)
DictWriter.writeheader()
DictWriter.writerows(self.data_list)
def main(self):
url_list = [
'http://www.weather.com.cn/textFC/hb.shtml',
'http://www.weather.com.cn/textFC/db.shtml',
'http://www.weather.com.cn/textFC/gat.shtml'
]
# 遍历url_list 爬取每一个区域的天气数据
for url in url_list:
# 获取网页源码html
html = self.get_html(url)
# 解析网页源码 得到数据
self.parse_data(html)
# 把解析到的所有数据保存
self.save_data()
if __name__ == '__main__':
w = WeatherData()
w.main()
报错分析
bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: html5lib. Do you need to install a parser library?
bs4.FeaturNontFound bs4的特征没有找到
tree builder 树生成器
parser library 解析器库
分析得出:bs4的特征没有找到:找不到具有您请求功能的树生成器:html5lib。您需要安装解析器库吗?
居然:那这是不是安装一下就行了?
hacker:真聪明👀👀👀
解决方案
只需要pip install html5lib即可完美解决
下载后运行写入csv结果如下:
结束语🏆🏆🏆
🔥推荐一款模拟面试、刷题神器网站
点击链接注册即可
1、算法篇(398题):面试必刷100题、算法入门、面试高频榜单
2、SQL篇(82题):快速入门、SQL必知必会、SQL进阶挑战、面试真题
3、大厂笔试真题:字节跳动、美团、百度、腾讯…
相关文章
- 解决Jenkins的错误“The Server rejected the connection: None of the protocols were accepted”
- docker logs命令查看容器日志的时候,如何将标准输出和错误输出进行区分查看?
- 报错:The specified datastore driver ("com.mysql.jdbc.Driver") was not found in the CLASSPATH. Please check your CLASSPATH specification, and the name of the driver.
- 史上最详细 Python第三方库添加方法 and 错误解决方法
- sqlplus登录提示:ORA-12162:TNS:net service name is incorrectly specified错误
- ORA-01555错误总结(二)
- 记一次ftp服务器错误 centOS 6.4 vsftpd 500 illegal port command
- 【Vue】错误 : 无法加载文件 C:UsersAdministratorAppDataRoamingnpmvue.ps1,因为在此系统上禁止运行脚本的解决方法
- The platform of the target `Pods` (iOS 4.3) is not compatible 错误
- 一则> ORA-00936: missing expression 错误的排查处理 SQL中的中括号对应ORACLE中的双引号
- Linux有问必答:Linux上Apache错误日志的位置在哪里?
- eclipse导入maven项目报错"Missing artifact com.sun.:tools:jar:1.8.0"错误解决方法
- Error java 错误 不支持发行版本5 ( 完美解决版)
- Kubernetes-kubectl命令出现错误【The connection to the server localhost:8080 was refused - did you specif...
- Android 读写位于SD卡上的sqlite数据库文件错误问题
- 使用 @Transactional 时常犯的N种错误
- 解决devenv.exe应用程序错误,应用程序发生异常
- this class is not key value coding-compliant for the key XXX错误的解决方法
- git clone: HTTP Basic: Access denied 错误
- Nginx_HTTP 499 状态码 nginx下 499错误
- Spring_错误 java.sql.SQLException: Lock wait timeout exceeded | CannotAcquireLockException 的解决
- 今日资料分析错误总结
- MyBatis报错:Invalid bound statement (not found)错误的可能原因
- 金仓kingbase连接报错The authentication type 10 is not supported. Check that you have configured the sys_hba.conf file to include the client's IP address or subnet
- nexus启动报错----->错误 1067: 进程意外终止。
- Unity 报错之 The type or namespace name ‘UI‘ does not exist in the namespace ‘UnityEngine‘
- The total number of locks exceeds the lock table size,mysql update报错
- Beyond Compare 提示错误"这个授权密钥已被吊销"的解决方法
- Python里面的种种错误类型
- vsCode 打开界面报错,尝试在目标目录创建文件时发生一个错误