Python 3 UnicodeDecodeError:' charmap'编解码器不能解码字节0x9d

时间:2015-06-10 08:18:57

标签: python unicode

我想制作搜索引擎,并在一些网站上关注教程。 我想测试解析html

from bs4 import BeautifulSoup

def parse_html(filename):
    """Extract the Author, Title and Text from a HTML file
    which was produced by pdftotext with the option -htmlmeta."""
    with open(filename) as infile:
        html = BeautifulSoup(infile, "html.parser", from_encoding='utf-8')
        d = {'text': html.pre.text}
        if html.title is not None:
            d['title'] = html.title.text
        for meta in html.findAll('meta'):
            try:
                if meta['name'] in ('Author', 'Title'):
                    d[meta['name'].lower()] = meta['content']
            except KeyError:
                continue
        return d

parse_html("C:\\pdf\\pydf\\data\\muellner2011.html")

它出错了

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 867: character maps to <undefined>enter code here

我使用encode()在Web上看到了一些解决方案。但我不知道如何在代码中插入encode()函数。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:47)

在Python 3中,文件以文本形式打开(解码为Unicode);你不需要告诉BeautifulSoup要解码的编解码器。

如果数据解码失败,那是因为您没有告诉open()调用在读取文件时要使用的编解码器;使用encoding参数添加正确的编解码器:

with open(filename, encoding='utf8') as infile:
    html = BeautifulSoup(infile, "html.parser")

否则将使用您的系统默认编解码器打开该文件,该编解码器取决于操作系统。