T
T
tasty692021-08-16 19:12:22
Python
tasty69, 2021-08-16 19:12:22

python encoding error?

I practice writing a parser, everything goes fine until the moment I work with the file:

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()


Throws an error:
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
help please

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question