-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.py
More file actions
275 lines (238 loc) · 10.4 KB
/
extractor.py
File metadata and controls
275 lines (238 loc) · 10.4 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'''
You will have to fill the functions below, with necessary extraction scripts.
Each function returns a specific information about the product.
The return value will always be a string or a list of strings.
Please check the accompanying test module if you would like to see what output is expected.
'''
import urllib
import re
_PREFIX_ = "ABHINAV SINHA"
def find_product_title(filename):
'''finds product title'''
if filename == "product_pages/product1.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<h1 class="title" itemprop="name">(.+?)</h1>'
pattern = re.compile(regex)
title = "'"+''.join(re.findall(pattern,htmltext))+"'"
return title
elif filename == "product_pages/product2.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<meta name="keywords" content="(.+?)" />'
pattern = re.compile(regex)
title = "'"+''.join(re.findall(pattern,htmltext))+"'"
return title
elif filename == "product_pages/product3.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<h2 id="product-title">(.+?)</h2>'
pattern = re.compile(regex)
title = "'"+''.join(re.findall(pattern,htmltext))+"'"
return title
elif filename == "product_pages/product4.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<a id="AuthorArtistTitle_productTitle" name="productTitle" class="ProductTitle" style="text-decoration:none;" itemprop="name">(.+?)</a>'
pattern = re.compile(regex)
title = "'"+''.join(re.findall(pattern,htmltext))+"'"
return title
elif filename == "product_pages/product5.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<h2 class="name">(.+?)</h2>'
pattern = re.compile(regex)
title = "'"+''.join(re.findall(pattern,htmltext))+"'"
return title
def find_product_price(filename):
'''finds product price'''
if filename == "product_pages/product1.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<meta itemprop="price" content="(.+?)">'
pattern = re.compile(regex)
price = "Rs." + re.findall(pattern,htmltext)[0]
return price
elif filename == "product_pages/product2.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<span id="sec_discounted_price_180624">(.+?)</span>'
pattern = re.compile(regex)
price = "Rs." + re.findall(pattern,htmltext)[0]
return price
elif filename == "product_pages/product3.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<label id="price" class="visible"> <span class="WebRupee">Rs</span>(.+?) </label>'
pattern = re.compile(regex)
price = "Rs." + re.findall(pattern,htmltext)[0]
return price
elif filename == "product_pages/product4.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<span itemprop="highPrice">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)[0]
return price
elif filename == "product_pages/product5.html":
htmlfile = urllib.urlopen(filename)
htmltext = htmlfile.read()
regex = '<strong data-price="17.7000">US(.+?)</strong>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)[0]
return price
def find_product_discount(filename):
'''finds product discount'''
htmlfile = urllib.urlopen(filename)
if filename == "product_pages/product1.html":
htmltext = htmlfile.read()
regex = '<span class="price">Rs. (.+?)</span>'
pattern = re.compile(regex)
retailprice = re.findall(pattern, htmltext)
regex = '<span class="selling-price[^.]*>Rs. (.+?)</span>'
pattern = re.compile(regex)
sellprice = re.findall(pattern, htmltext)
discount = str(int(retailprice[0]) - int(sellprice[0]))
return discount
elif filename == "product_pages/product5.html":
htmltext = htmlfile.read()
regex = '<strong data-price="17.7000">(.+?)</strong>'
pattern = re.compile(regex)
price = re.findall(pattern, htmltext)
nprice = float(price[0].replace("US$",""))
regex = '<dd>US$(.+?)</dd></dl>'
pattern = re.compile(regex)
price2 = re.findall(pattern, htmltext)
price2 = 53.35
discount = "$"+str(price2 - nprice)
return discount
def find_product_sizes(filename):
'''finds product sizes'''
htmlfile = urllib.urlopen(filename)
if filename == "product_pages/product1.html":
htmltext = htmlfile.read()
sizes = []
regex = '<span>(.+?)</span>'
pattern = re.compile(regex)
sizes = re.findall(pattern,htmltext)
sizes = filter(lambda i: str.isdigit(i), sizes)
return sizes
elif filename == "product_pages/product3.html":
htmltext = htmlfile.read()
sizes = []
size = []
regex = '<span>(.+?)</span>'
pattern = re.compile(regex)
size = re.findall(pattern,htmltext)
size = filter(lambda i: str.isdigit(i), size)
for item in size:
if item not in sizes:
sizes.append(item)
return sizes
elif filename == "product_pages/product4.html":
htmltext = htmlfile.read()
sizes = []
regex = '<option class="piAvailable" id="a1837_0">(.+?)</option><option class="piAvailable" id="a1837_1">(.+?)</option><option class="piAvailable" id="a1837_2">(.+?)</option>'
pattern = re.compile(regex)
sizes = re.findall(pattern,htmltext)
return sizes
elif filename == "product_pages/product5.html":
htmltext = htmlfile.read()
sizes = []
regex = '<span id="premiumList_option_0">Style:TNW-S0305 / Color:Blue / Size:(.+?)'
pattern = re.compile(regex)
sizes = re.findall(pattern,htmltext)
return sizes
def find_product_fabric(filename):
'''finds product fabric'''
htmlfile = urllib.urlopen(filename)
if filename == "product_pages/product1.html":
htmltext = htmlfile.read()
regex = 'Fabric: 100% (.+?) Regular FitFull Sleeves</div>'
pattern = re.compile(regex)
fabric = str.lower(re.findall(pattern,htmltext)[0])
return fabric
elif filename == "product_pages/product2.html":
htmltext = htmlfile.read()
regex = '<p><br />Set Of 3 Round Neck T-shirts<br />Material: 100% (.+?)<br />Colors : Red , White and Blue will be sent</p>'
pattern = re.compile(regex)
fabric = str.lower(re.findall(pattern,htmltext)[0])
return fabric
elif filename == "product_pages/product3.html":
htmltext = htmlfile.read()
regex = '<b>Fabric: 100% (.+?)</b>'
pattern = re.compile(regex)
fabric = str.lower(re.findall(pattern,htmltext)[0])
return fabric
elif filename == "product_pages/product4.html":
htmltext = htmlfile.read()
regex = '<td>Fit: Classic Fit. Materials: Soft Stretchy (.+?) Fabrics'
pattern = re.compile(regex)
fabric = str.lower(re.findall(pattern,htmltext)[0])
return fabric
elif filename == "product_pages/product5.html":
htmltext = htmlfile.read()
regex = '<th>Material(.+?)</th>'
pattern = re.compile(regex)
fabric = ''.join(re.findall(pattern,htmltext))
return fabric
def find_product_stylecode(filename):
'''finds product stylecode'''
htmlfile = urllib.urlopen(filename)
if filename == "product_pages/product1.html":
htmltext = htmlfile.read()
regex = '<meta name="og_image" property="og:image" content="http://img5a.flixcart.com/image/shirt/w/x/c/(.+?)-zovi-46-original-imadxwahmbsmxyyw.jpeg"/>'
pattern = re.compile(regex)
stylecode = re.findall(pattern,htmltext)[0]
return stylecode
elif filename == "product_pages/product2.html":
htmltext = htmlfile.read()
regex = 'SCIN : (.+?)</span>'
pattern = re.compile(regex)
stylecode = re.findall(pattern,htmltext)[0]
return stylecode
elif filename == "product_pages/product4.html":
htmltext = htmlfile.read()
regex = '<b>Mfg Part#: </b>rlms(.+?)custom'
pattern = re.compile(regex)
stylecode = re.findall(pattern,htmltext)[0]
return stylecode
elif filename == "product_pages/product5.html":
htmltext = htmlfile.read()
regex = '<link rel="canonical" href="http://list.qoo10.com/item/[^.]*SHIRTS-KOREA/(.+?)" />'
pattern = re.compile(regex)
stylecode = re.findall(pattern,htmltext)[0]
return stylecode
def find_product_gender(filename):
'''finds product gender'''
htmlfile = urllib.urlopen(filename)
if filename == "product_pages/product1.html":
htmltext = htmlfile.read()
regex = '<a class="link fk-inline-block" href="/(.+?)s-clothing/'
pattern = re.compile(regex)
gender = re.findall(pattern,htmltext)[0]
return gender
elif filename == "product_pages/product2.html":
htmltext = htmlfile.read()
regex = 'Home:Fashion:(.+?)'s Apparel'
pattern = re.compile(regex)
gender = str.lower(re.findall(pattern,htmltext)[0])
return gender
elif filename == "product_pages/product3.html":
htmltext = htmlfile.read()
regex = '<li id="size-chart" data-tag="(.+?)s-formal-shirts-sizechart" class="true">'
pattern = re.compile(regex)
gender = re.findall(pattern,htmltext)[0]
return gender
elif filename == "product_pages/product4.html":
htmltext = htmlfile.read()
regex = '<td id="pAttributes_rptProdAttributes_attrValue_0" align="left">(.+?)</td>'
pattern = re.compile(regex)
gender = re.findall(pattern,htmltext)[0]
return gender
elif filename == "product_pages/product5.html":
htmltext = htmlfile.read()
regex = '<meta name="keywords" content="(.+?) Clothing,Tops / Shirts,Long Sleeve, Qoo10, Shopping, Openmarket, Auction, Market, Discount, Shopping online" />'
pattern = re.compile(regex)
gender = re.findall(pattern,htmltext)[0]
return gender