python - 如何在 Beautiful Soup 中包含未定义的字符?
问题描述
我正在使用 Beautiful Soup 库通过 Python 脚本解析 HTML 文件。我不断收到错误:
Exception has occurred: UnicodeDecodeError
'charmap' codec can't decode byte 0x81 in position 76701: character maps to undefined
from bs4 import BeautifulSoup
with open('playlist.html', 'r') as html_file: #this will read the HTML file. 1) specify the target, 2) r command allows us to read
content = html_file.read()
soup = BeautifulSoup(content, 'lxml')
anchor_html_tags = soup.find_all('span')
for anchor in anchor_html_tags:
print(anchor.text)
我用我保存的其他 HTML 文件尝试了这个,但没有遇到这个问题,作为参考,我使用的页面是一个下载的网页。我还尝试安装似乎没有解决任何问题的 chardet。任何建议将不胜感激。
解决方案
推荐阅读
- java - 我在邮递员和浏览器中收到 500 错误,但控制器在调试时返回正确的对象
- python - Selenium 和 BeautifulSoup 无法获取所有 HTML 内容
- asp.net-core - 为什么我的登录不适用于我的 mvc Web 应用程序
- r - 根据R中另一行的条件改变新列
- python - 为什么 python 不能从我创建的模块中导入一个简单的函数?
- amazon-web-services - 如何为 API Gateway RestAPI 禁用区分大小写的路径段
- html - 如何将一个标签与另一个多行标签对齐?
- python - 谷歌趋势分类搜索
- java - 将迭代器的迭代器转换为列表?
- python-3.x - 提高读取多个大文件的性能