python - Unicode解码错误: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

标签 python utf-8 character-encoding

<分区>

我正在尝试通过一个大胆的类(class)在 python 中制作一个爬虫。我有这个方法 get_page() 返回页面的内容。

def get_page(url):
    '''
    Open the given url and return the content of the page.
    '''

    data = urlopen(url)
    html = data.read()
    return html.decode('utf8')

原来的方法只是返回data.read(),但是那样我就不能做像str.find()这样的操作了。快速搜索后,我发现我需要解码数据。但是现在我得到了这个错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

我在 SO 中发现了类似的问题,但没有一个是专门针对此的。请帮忙。

最佳答案

您正在尝试解码无效的字符串。

任何有效的 UTF-8 字符串的起始字节必须在 0x000x7F 的范围内。 所以0x8B肯定是无效的。 来自 RFC3629 Section 3 :

In UTF-8, characters from the U+0000..U+10FFFF range (the UTF-16 accessible range) are encoded using sequences of 1 to 4 octets. The only octet of a "sequence" of one has the higher-order bit set to 0, the remaining 7 bits being used to encode the character number.

您应该发布您尝试解码的字符串。

关于python - Unicode解码错误: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41206304/

相关文章:

mysql - mysql中非utf8列的建议字符集

javascript - 将 html 附加到 dom 不尊重字符集

python - 如何用 Python 在一行中测试多个模值?

python - 如何使用 OpenCV 理解 Python 中的头部姿势估计角度?

python - Python 的日志记录 SMTP 处理程序会卡住我的线程 2 分钟吗?

python - 如何在 SQLAlchemy 模型中获取列和值字典?

java - 如何应用 utf-8 字符集编码将 DOM 文档对象转换为 xml

python - 在python 3中将utf-8编码的字符串转换为纯文本

java - java中的字节流

java - 无法使用 LIKE 从 PostgreSQL 选择 Unicode 数据