-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.py
More file actions
237 lines (184 loc) · 7.32 KB
/
Script.py
File metadata and controls
237 lines (184 loc) · 7.32 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Using beautifulsoup for web scrapping
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
## test
# try reading github file
url = "https://raw.githubusercontent.com/ys3006/Art-parser/master/data/2015-03-18/lot1.html"
# open the html, grabbing the page
uOpen = uReq(url)
page_html = uOpen.read()
# close the connection
uOpen.close()
# html parser
page_soup = soup(page_html, 'html.parser')
# grab author name
page_soup.findAll('h2')[0].text
## Task 1 - artist + works
import glob
artist_container = []
works_container = []
price_container = []
for filename in glob.glob('/Users/Yifan/Downloads/lot-parser/data/2015-03-18/*.html'):
page_soup = soup(open(filename, "r").read(), 'html5lib')
containers = page_soup.findAll('body')
for container in containers:
artist = container.findAll('h2')[0]
artist_container.append(artist.text)
work = container.findAll('h3')[0]
works_container.append(work.text)
price = container.findAll('div')[1]
price_container.append(price.text)
print(artist_container)
print(works_container)
print(price_container)
## Output - artist + works
# print output for 2015-03-18 directory
print('[')
for artist, works in zip(artist_container, works_container):
out = (' {' + '\n'
+ ' artist: ' + "'" + artist + "'" + ', ' + '\n'
+ ' works: [' + works + "']," + '\n'
+' },' + '\n')
print(out)
print(']')
## Output - artist + works + price
# print output for 2015-03-18 directory
print('[')
for artist, works, price in zip(artist_container, works_container, price_container):
out = (' {' + '\n'
+ ' artist: ' + "'" + artist + "'" + ', ' + '\n'
+ ' works: ' + '[' + '\n'
+ ' { ' + "title: '" + works + "', price: '" + price + "' }," + '\n'
+' },' + '\n')
print(out)
print(']')
# separate currency and amount from pirce container
sep = []
for word in price_container:
line = word.split(' ')
sep.append(line)
print(sep)
# store currency and amount in two lists: currency_container and price_container2
currency_container = []
price_container2 = []
for item in sep:
curr = item[0]
num = item[1]
currency_container.append(curr)
price_container2.append(num)
print(currency_container)
print(price_container2)
## Output - artist + works + currency + price amount
# print output for 2015-03-18 directory
print('[')
for artist, works, currency, price in zip(artist_container, works_container, currency_container, price_container2):
out = (' {' + '\n'
+ ' artist: ' + "'" + artist + "'" + ', ' + '\n'
+ ' works: ' + '[' + '\n'
+ ' { ' + "title: '" + works + "', currency: '" + currency + "', amount: '" + price + "' }," + '\n'
+' },' + '\n')
print(out)
print(']')
#------ Task 1~4: 20 min ------#
## Task 5 - Grab from 2017-12-20 directory
artist2_container = []
works2_container = []
currency2_container = []
price2_container = []
for filename in glob.glob('/Users/Yifan/Downloads/lot-parser/data/2017-12-20/*.html'):
page_soup = soup(open(filename, "r").read(), 'html5lib')
for container in containers:
artist = page_soup.findAll('h3', {'class': 'artist'})[0]
artist2_container.append(artist.text)
work = page_soup.findAll('h3')[1]
works2_container.append(work.text)
currency = page_soup.findAll('span', {'class': 'currency'})[0]
currency2_container.append(currency.text)
price = page_soup.findAll('span')[1]
price2_container.append(price.text)
print(artist2_container)
print(works2_container)
print(currency2_container)
print(price2_container)
## Output - Completed output from 2017-12-20 directory
print('[')
for artist, works, currency, price in zip(artist2_container, works2_container, currency2_container, price2_container):
out = (' {' + '\n'
+ ' artist: ' + "'" + artist + "'" + ', ' + '\n'
+ ' works: ' + '[' + '\n'
+ ' { ' + "title: '" + works + "', currency: '" + currency + "', amount: '" + price + "' }," + '\n'
+' },' + '\n')
print(out)
print(']')
## Task 6 - Convert the amount of all works to USD
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
rate = 1.34 # 1 GBP = 1.34 USD
# Since only for data from 2015-03-18 has GBP currency
for i in range(len(currency_container)):
if currency_container[i] == 'GBP':
# convert string with commas to int for multiply
price_container2[i] = locale.atoi(price_container2[i]) * rate
# convert int to string with commas as thousands separators
price_container2[i] = "{:,}".format(price_container2[i])
currency_container[i] = 'USD'
# For final task, given that some of artists have multiple works;
# since artist names is unique, to return all of the work for each artist,
# consider using dictionary, where key is the artist name
## Final Task I - Preprocessing
# Before creating dictionary, I found artist name from two directories not in the same format,
# some of them along with (birth year - death year),
# clean the artist name format by removing the date for next step of pairing keys
import re
# remove everything inside brackets together with "()", and taking out the space at begining and end
for i in range(len(artist_container)):
artist_container[i] = re.sub(r'\(.*?\)', '', artist_container[i]).strip()
for i in range(len(artist2_container)):
artist2_container[i] = re.sub(r'\(.*?\)', '', artist2_container[i]).strip()
# To iterate through outputs from two directories, combine dataset together
Artist = artist_container + artist2_container
Works = works_container + works2_container
Currency = currency_container + currency2_container
Price = price_container2 + price2_container
## Final Task II - Create dictionary
# D is dict for storing key(artist name): [title1, currency1, amount1], [title2, currency1, amount1]
D = {}
for i in range(len(Artist)):
key = Artist[i]
# if key not in dict, add them in
if key not in D.keys():
value = [(Works[i], Currency[i], Price[i])]
D[key] = value
# if key exists, add value under that key
else:
value2 = (Works[i], Currency[i], Price[i])
D[key].append(value2)
## Final Task III - Return total value(T) for each artist
# T is dict for storing totalValue for each key from D above
T = {}
for key in D.keys():
value = D[key]
totalValue = 0
for j in range(len(value)):
V = value[j][2]
totalValue += float(V.replace(',',''))
T[key] = str(totalValue)
## Final Output - artist + totalValue + title + currency + totalLifetimeValue
# print out final format
print('[')
for key in D.keys():
value = D[key]
totalValue = T[key]
outer_frame = (' {' + '\n'
+ " artist: '" + key + "'," + '\n'
+ " totalValue: 'USD " + totalValue + "'," + '\n'
+ " works: [")
print(outer_frame)
for j in range(len(value)):
inner_frame = (" { title: '" + value[j][0]
+ "', currency: '" + value[j][1]
+ "', totalLifetimeValue: '" + value[j][2] + "' },")
print(inner_frame)
print(" ]," + '\n' + " },")
print(']')