首页 > 解决方案 > 如何在 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。任何建议将不胜感激。

标签: pythonbeautifulsoup

解决方案


推荐阅读