Answer the question
In order to leave comments, you need to log in
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()
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