'charmap'编解码器无法解码位置33222中的字节0x8d:字符映射为<undefined>

时间:2019-12-22 13:42:10

标签: python html encoding beautifulsoup lxml

我正在尝试通过BeautifulSoup用lxml解析一个很长的html文件。我知道html文件的字符编码为UTF-8 with BOM,但是每当我尝试运行contents = f.read()时,都会出现以下错误:

'charmap' codec can't decode byte 0x8d in position 33222: character maps to <undefined>
< br />这是我的代码的第一个(也是有问题的)位:

from bs4 import BeautifulSoup

with open("doc.html", "r") as f:

    contents = f.read()

    soup = BeautifulSoup(contents, 'lxml')

    print(soup.h2)
    print(soup.head)
    print(soup.li)

这是错误显示:

    UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-1-4805460879e0> in <module>
      3 with open("doc.html", "r") as f:
      4 
----> 5     contents = f.read()
      6 
      7     soup = BeautifulSoup(contents, 'lxml')

~\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 33222: character maps to <undefined>

1 个答案:

答案 0 :(得分:0)

with open("doc.html", "r", encoding="UTF-8") as f应该可以解决您的问题。