python - 当我尝试抓取 web 数据 : module 'html5lib.treebuilders' has no attribute '_base' 时弹出以下错误

标签 python web-scraping beautifulsoup export-to-csv

我正在尝试使用 beautiful soup 在 python 中进行网络抓取,作为一个新手,我从 [https://syntaxbytetutorials.com/beautifulsoup-4-python-web-scraping-to-csv-excel-file/] 获取了源代码。并开始试验。 现在,我有一个错误

module 'html5lib.treebuilders' has no attribute '_base'`

如果有人向我解释错误背后的原因并提供解决方案,那将非常有帮助 :)

import urllib.request as urllib
import csv
import re
from bs4 import BeautifulSoup
 
rank_page = 'https://socialblade.com/youtube/top/50/mostviewed'
request = urllib.Request(rank_page, headers={'User-Agent':'Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'})
page = urllib.urlopen(request)
soup = BeautifulSoup(page, 'html.parser')
 
channels = soup.find('div', attrs={'style': 'float: right; width: 900px;'}).find_all('div', recursive=False)[4:]
 
file = open('topyoutubers.csv', 'wb')
writer = csv.writer(file)
 
# write title row
writer.writerow(['Username', 'Uploads', 'Views'])
 
for channel in channels:
    username = channel.find('div', attrs={'style': 'float: left; width: 350px; line-height: 25px;'}).a.text.strip()
    uploads = channel.find('div', attrs={'style': 'float: left; width: 80px;'}).span.text.strip()
    views = channel.find_all('div', attrs={'style': 'float: left; width: 150px;'})[1].span.text.strip()
 
    print (username + ' ' + uploads + ' ' + views)
    writer.writerow([username.encode('utf-8'), uploads.encode('utf-8'), views.encode('utf-8')])
 
file.close()

最佳答案

尝试用“_html5lib.py”中的“base”替换字符串“_base”。

你能显示错误的回溯吗?错误来自哪一行或文件。

关于python - 当我尝试抓取 web 数据 : module 'html5lib.treebuilders' has no attribute '_base' 时弹出以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63438909/

相关文章:

python - 如何在linux机器上安装Scrapy

python - 带有多个蜘蛛和 CSVItemExporter 的 Scrapy CSV 标题行格式

python - Beautifulsoup 不返回子元素

python - BeautifulSoup 处理多个 .html 文件

python - 在 Odoo 11 上基于 SQL 查询构建新 View 或模型

python - 简单的 Django 表单

python - python中的代数表达式

Python+Chaco+Traits - 渲染错误 : unexpected fills of line plot of large data?

python - Scrapy 是否可以从原始 HTML 数据中获取纯文本?

python - 如何使用 For 循环从 html 获取多个链接?