zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【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、大厂笔试真题:字节跳动、美团、百度、腾讯…

在这里插入图片描述