Python 2.7,解码问题 ('utf-8' )

标签 python utf-8

我有:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib2 import urlopen

page2 = urlopen('http://pogoda.yandex.ru/moscow/').read().decode('utf-8')

page = urlopen('http://yasko.by/').read().decode('utf-8')

在“page ...”行中,我有错误“UnicodeDecodeError:'utf8'编解码器无法解码位置32中的字节0xc3:无效的连续字节”,但在“page2 ...”行中,错误不是,为什么?

从yasko.by中的32位置开始西里尔字母符号,我如何正确地得到它?

谢谢!

最佳答案

http://yasko.by/的内容使用 windows-1251 进行编码,而 http://pogoda.yandex.ru/moscow/ 的内容使用 utf-8 编码。

page = .. 行应变为:

page = urlopen('http://yasko.by/').read().decode('windows-1251')

关于Python 2.7,解码问题 ('utf-8' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910502/

相关文章:

ruby - 从 1.9.3 开始,如何在 Ruby 中获取 YAML 以将 ASCII-8Bit 字符串转储为字符串?

Mysql 使用 CHAR 或 BLOB 存储已知字节大小限制的 UTF-8 字符串

javascript - JSON.parse - SyntaxError : Unexpected token & at Object. 解析( native )

python - 在 numpy 中执行多种方式

python - Pandas - 按单元格中的项目数拆分列

java - 从 Google Cloud pub/sub 反序列化 JSON 并保留 UTF-8 编码

c - 如何在 C 中将 UTF-8 字符转换为二进制

python - 如何创建 SWIG 接口(interface)文件?

Python - 属性错误 : 'module' object has no attribute 'QueryFrame'

python - Python 中使用 lambda + filter 进行列表过滤