forked from willpennell/Genre_Identifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscogs_Webscrape.py
181 lines (107 loc) · 4.55 KB
/
Discogs_Webscrape.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import requests, bs4
import re
import pandas as pd
from tkinter.filedialog import askopenfilename
#song = input('Enter your song: ')
#song2 = song.replace(" ","+")
#artist = input('Enter artist: ')
#artist2 = artist.replace(" ","+")
filepath = askopenfilename()
Songspd = pd.read_csv(filepath)
discogs_url = 'https://www.discogs.com/'
# In[5]:
# defining final df
dfinal = []
# per song, begin the genre & style scrape
rows = Songspd.values.tolist()
for row in rows:
song = row[0]
artist = row[1]
song2 = song.replace(" ","+")
artist2 = artist.replace(" ","+")
url = f'https://www.discogs.com/search/?q={song2}&type=release'
url2 = f'https://www.discogs.com/search/?q={song2}+{artist2}&type=release'
# Page 1
discogs = requests.get(url)
discogs.raise_for_status()
bSoup = bs4.BeautifulSoup(discogs.text, 'html.parser')
#Shortening html to Releases only
releases=bSoup.find_all('div', {"data-object-type": "release"})
urllist = []
#url parsing - url1, checking songname and artistname of each release
for release in releases:
if len(urllist) == 3:
break
songnametag = release.findChild("h4" , recursive=False)
artistnametag = release.findChild("h5" , recursive=False)
if songnametag.find('a', text=re.compile(song + r'.*')):
if artistnametag.find('a', text=re.compile(artist + r'.*')):
linkElem = release.select('h4>a')
new_url = linkElem[0].get('href')
discogs_url_1 = discogs_url + new_url
urllist.append(discogs_url_1)
#if list is empty, try url2
if len(urllist) == 0:
url = url2
discogs = requests.get(url)
discogs.raise_for_status()
bSoup = bs4.BeautifulSoup(discogs.text, 'html.parser')
releases=bSoup.find_all('div', {"data-object-type": "release"})
#creating a list of all releases that have the correct songname and artistname
for release in releases:
if len(urllist) == 3:
break
songnametag = release.findChild("h4" , recursive=False)
artistnametag = release.findChild("h5" , recursive=False)
if songnametag.find('a', text=re.compile(song + r'.*')):
if artistnametag.find('a', text=re.compile(artist + r'.*')):
linkElem = release.select('h4>a')
new_url = linkElem[0].get('href')
discogs_url_1 = discogs_url + new_url
urllist.append(discogs_url_1)
#take first three matching realease urls
urlnames = urllist[0:3]
#Defining Genre & Style List
Genrelist =[]
Stylelist =[]
for urlname in urlnames:
# Load each Release page
discogs = requests.get(urlname)
discogs.raise_for_status()
# Shortening html to Release Header only
bSoup = bs4.BeautifulSoup(discogs.text, 'html.parser')
releaseheader=bSoup.find("div", {"id": "release-header"})
#Genre
Genres = releaseheader.select('a[href^="/genre/"]')
for Genre in Genres:
genrename = Genre.contents
Genrelist.append(genrename[0])
# Style
Styles = releaseheader.select('a[href^="/style/"]')
for Style in Styles:
stylename = Style.contents
Stylelist.append(stylename[0])
# Unique values for Genre and Styles
Genrelist = set(Genrelist)
Genrelist = (list(Genrelist))
Stylelist = set(Stylelist)
Stylelist = (list(Stylelist))
# creating df, Splitting Genres and Styles to seperate columns in df
d1 = pd.DataFrame({'url1':urlnames[0],'url2':urlnames[1],'url3':urlnames[2], 'Song_Title':song,'Artist':artist,'Genres':[Genrelist],'Styles':[Stylelist]})
d2 = (pd.DataFrame(d1.pop('Genres').values.tolist(), index=d1.index)
.rename(columns = lambda x: 'Genre_{}'.format(x+1)))
d2
d3 = (pd.DataFrame(d1.pop('Styles').values.tolist(), index=d1.index)
.rename(columns = lambda x: 'Style_{}'.format(x+1)))
d3
df = d1.join(d2)
df4 =df.join(d3)
#Appending each output df and merging to into one final df
dfinal.append(df4)
df = pd.concat(dfinal)
df
df.to_csv('Genres & Styles Output.csv',index=False)
# In[ ]: