Answer the question
In order to leave comments, you need to log in
Parsing in python, what's wrong?
import requests
user_id = 12345
url = 'https://yandex.ru/'
r = requests.get(url)
with open('test.html', 'w') as output_file:
output_file.write(r.text.encode('cp1251'))
Answer the question
In order to leave comments, you need to log in
The root of the problem is that you are trying to convert to cp1251 text that contains characters that are not represented in this encoding. In addition, even if you succeeded in transcoding, the encode method would return bytes, and you opened the file in text mode, another error would occur. You should probably write like this:
with open('test.html', 'w', encode='utf-8') as output_file:
output_file.write(r.text)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question