-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_songs.py
33 lines (26 loc) · 1.01 KB
/
best_songs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
import urllib.request as ureq
from bs4 import BeautifulSoup as bs
import pandas as pd
def sort_tracks():
best_songs_url = "https://pitchfork.com/features/lists-and-guides/best-songs-2021/"
content = ureq.urlopen(best_songs_url).read()
soup = bs(content, "html.parser")
headers = soup.find_all('h2')
sort_tracks.tracks = list(map(lambda h: h.text.strip(), headers))
def rank_list():
global rank_artist_track_list
list = pd.DataFrame(sort_tracks.tracks)
clean_list = pd.DataFrame(list[0].str.split(':', expand=True).values, columns=['artist', 'track'])
clean_list['track'] = clean_list['track'].map(lambda x: x[1:-1].replace('“', ''))
result = []
total_rank = len(clean_list)
for row in clean_list['track']:
rank = total_rank
total_rank -= 1
result.append(rank)
clean_list['rank'] = result
sort_list = clean_list[['rank','artist','track']]
rank_artist_track_list = sort_list.values.tolist()
sort_tracks()
rank_list()