【发布时间】:2014-10-30 05:07:59
【问题描述】:
代码不言自明...
$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:18)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request as req
>>> url = 'http://bangladeshbrands.com/342560550782-44083.html'
>>> res = req.urlopen(url)
>>> html = res.read()
>>> type(html)
<class 'bytes'>
>>> html = html.decode('utf-8') # bytes -> str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 66081: invalid start byte
【问题讨论】:
-
你为什么不首先使用知道如何通过 HTTP 正确处理 HTML 的模块?
-
@IgnacioVazquez-Abrams,你能解释一下吗? read() 方法对大多数 url 都正常工作。
-
read()方法不会为您提供有关服务器告诉您 HTML 字符集是什么的任何信息。 -
@IgnacioVazquez-Abrams,您能提出任何替代解决方案吗?
标签: python-3.x character-encoding