<p>看起来原始文件是UTF-16格式的。你知道吗</p>
<p>无论出于何种原因,<code>BeautifulSoup(..., from_encoding='utf-16le')</code>都不理解这种情况,但是您可以通过在将文件传递给BS之前手动读取和解码文件来解决这个问题。你知道吗</p>
<p>下面是我创建一个UTF-16LE的HTML文件的脚本,转储它的内容,尝试将它直接传递到BS4,最后使用上面描述的解决方法。你知道吗</p>
<pre><code>$ echo '<html><div>hello</div></html>' | iconv -f utf-8 -t utf-16le > y.html
$ file y.html
$ xxd y.html
00000000: 3c00 6800 7400 6d00 6c00 3e00 3c00 6400 <.h.t.m.l.>.<.d.
00000010: 6900 7600 3e00 6800 6500 6c00 6c00 6f00 i.v.>.h.e.l.l.o.
00000020: 3c00 2f00 6400 6900 7600 3e00 3c00 2f00 <./.d.i.v.>.<./.
00000030: 6800 7400 6d00 6c00 3e00 0a00 h.t.m.l.>...
$ python
>>> import bs4
>>> s = bs4.BeautifulSoup(open('y.html'))
&lt;html&gt;&lt;div&gt;hello&lt;/div&gt;&lt;/html&gt;
>>> s = bs4.BeautifulSoup(open('y.html'), from_encoding='utf-16le')
&lt;html&gt;&lt;div&gt;hello&lt;/div&gt;&lt;/html&gt;
>>> s = bs4.BeautifulSoup(open('y.html'), 'html.parser', from_encoding='utf-16le')
&lt;html&gt;&lt;div&gt;hello&lt;/div&gt;&lt;/html&gt;
>>> d = open('y.html', 'rb').read().decode('utf-16le')
>>> d
'<html><div>hello</div></html>\n'
>>> s = bs4.BeautifulSoup(d)
>>> s
<html><div>hello</div></html>
>>>
</code></pre>