-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrap.py
54 lines (42 loc) · 1.63 KB
/
scrap.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import csv
import string
import sys
import requests
# some important modules to scrape a website.
from bs4 import BeautifulSoup
def scrap_letter(letter):
headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0"}
page = requests.get(f'http://www.jazzmusicarchives.com/ListArtistsAlpha.aspx?letter={letter}', headers=headers)
if page.status_code != 200:
sys.exit('Non 200 status code received')
soup = BeautifulSoup(page.content, 'html.parser')
artist = []
genres = []
countries = []
urls = []
for li in soup.find_all("li"):
a = li.find("a")
if not a.get("href", "").startswith("/artist/") or "#shout" in a.get("href", ""):
continue
span = li.find("span")
if " • " not in span.get_text():
continue
genre, country = span.get_text().split(" • ")
genres.append(genre.strip())
countries.append(country.strip())
art = a.get_text().strip()
if "," in art:
s = art.split(",")
art = f"{s[1]} {s[0]}".strip()
artist.append(art)
url = f"http://www.jazzmusicarchives.com{a['href']}"
urls.append(url)
# Save the dataset in csv file.
colums_names = ['artist', 'genre', 'country', 'url']
with open(f'./raw_{letter.upper()}_jazz_musicarchives.csv', 'w', encoding='utf-8') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(colums_names)
writer.writerows(list(set(zip(artist, genres, countries, urls))))
f.close()
for letter in string.ascii_lowercase[:26]:
scrap_letter(letter)