Skip to main content
 首页 » 编程设计

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

2024年11月01日11luoye11

我有:

#!/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')