2

I have:

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

And in line "page ..." I have error "UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 32: invalid continuation byte", but in line "page2 ..." th error is not, why?

From a position of 32 in yasko.by starts Cyrillic symbols, how I get it correctly?

Thanks!

1 Answer 1

2

The content of http://yasko.by/ is encoded with windows-1251, while the content of http://pogoda.yandex.ru/moscow/ is encoded with utf-8.

page = .. line should become:

page = urlopen('http://yasko.by/').read().decode('windows-1251')
Sign up to request clarification or add additional context in comments.

2 Comments

and instead "<title>Главная</title>" I have "<title>\u041e\u0428\u0418\u0411\u041a\u0410</title>" with .decode('windows-1251')
@user2350206, Non-ascii characters are represented as u'\uxxxx' form in Python 2.x. Printing it will show you what you expected: print(urlopen('http://yasko.by/').read().decode('windows-1251'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.