Практикуюсь в написании парсера, все идет отлично до момента работы с файлом:
import requests
from bs4 import BeautifulSoup
import csv
URL = 'https://megapesni.com/best_music_2021.html'
HEADERS = {'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'}
FILE = 'musics.csv'
def get_html(url, params = None):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='music-popular-wrapper')
musics = []
for item in items:
musics.append({
'name': item.find('a', class_='popular-play-composition').get_text(),
'composition' : item.find('a', class_='popular-play-author').get_text(),
})
#print(musics)
save_file(musics, FILE)
def save_file(items, path):
with open(path, 'w', newline='') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(['Автор', 'Трек'])
for item in items:
writer.writerow([item['name'], item['composition']])
def parse():
html = get_html(URL)
if html.status_code == 200:
get_content(html.text)
else:
print('Error')
parse()
Выдаёт ошибку:
Traceback (most recent call last):
File "parser.py", line 45, in
parse()
File "parser.py", line 40, in parse
get_content(html.text)
File "parser.py", line 25, in get_content
save_file(musics, FILE)
File "parser.py", line 31, in save_file
writer.writerow([item['name'], item['composition']])
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\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 '\u0306' in position 17: character maps to
Помогите пожалуйста