我正在尝试通过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>
答案 0 :(得分:0)
with open("doc.html", "r", encoding="UTF-8") as f
应该可以解决您的问题。