Answer the question
In order to leave comments, you need to log in
How to solve encoding problem when writing a file in Python?
I made a parser and I wanted the result to be saved in a .txt file, but when I try to save the file it gives me an error.
The code:
import requests
from bs4 import BeautifulSoup as sup
page = 1
while True:
response = requests.get("https://stopgame.ru/review/new/izumitelno/p" + str(page))
html = sup(response.content, "html.parser")
items = html.select(".items > .article-summary")
if (len(items)):
for elements in items:
title = elements.select(".caption > a")
res = title[0].text
with open("games_list.txt", "w") as file:
file.write(res)
page += 1
else:
break
Traceback (most recent call last):
File "c:\Users\1\Desktop\other_projects\bs4\main.py", line 16, in <module>
file.write(res)
File "C:\Users\1\AppData\Local\Programs\Python\Python310\lib\encodings\cp1251.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xf6' in position 1: character maps to <undefined>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question