homeplus.py
8.34 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
276
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import urllib.request
import time
def name_preprocessing(a):
if a[0] == ' ':
a = a[1:]
num = a.count('_')
for i in range(num):
a = a.replace('_', '')
if a.find('G') != -1:
temp = a.find('G')
if temp > 3:
for i in range(temp - 1, 0, -1):
if ord(a[i]) < 48 or ord(a[i]) > 57:
end = i
break
a = a[:end + 1]
if a.find('L') != -1:
if a.find('M') != -1:
temp = a.find("M")
for i in range(temp - 1, 0, -1):
if ord(a[i]) < 48 or ord(a[i]) > 57:
end = i
break
else:
temp = a.find('L')
for i in range(temp - 1, 0, -1):
if ord(a[i]) < 48 or ord(a[i]) > 57:
end = i
break
a = a[:end + 1]
if a[-1] == '.':
a = a[:-2]
temp1 = a.find('(')
temp2 = a.find(')')
if temp1 == 0:
a = a[temp2+1:]
return a
def unit_preprocessing(a, b):
if a[0] == ' ':
a = a[1:]
unit = a.find(b)
b = a[:unit]
temp = a.find('당')
won = a.find('원')
a = a[temp + 2:won]
if a.find('.') != -1:
won = a.find('.')
a = a[:won]
a = a.replace(',', '')
unit = int(int(a) / int(b) * 100)
return unit
def home_water(H):
page_num = 3
path = 'C:\chromedriver'
driver = webdriver.Chrome(path)
driver.get(
'http://www.homeplus.co.kr/app.exhibition.category.Category.ghs?comm=category.list&cid=60386&WT.ac=GNBctg_2dep_ctg')
for i in range(page_num-1):
driver.find_element_by_xpath("//a[@class='more']").click()
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
table = soup.find_all('ul', {'class' : 'fix-ty2'})
for five in table:
one = five.find_all('li')
for i in one:
temp_dict = {}
a = i.find('div', {'class' : 'exh-comtxt w-ty4'})
if type(a) == type(None):
continue
price = a.find('span', {'class' : 'price'})
unit = price.find('span', {'class' : 'unit-area'})
if type(unit) == type(None):
continue
price = price.find('strong', {'class' : 'buy'})
name = a.find('a', {'class' : 'name'})
if type(name) == type(None):
continue
name = name_preprocessing(name.text)
price = price.text
price = price.replace(',', '')
unit = unit_preprocessing(unit.text, 'ML')
overlap = False
change = 0
for qw in range(len(H)):
if H[qw]['name'] == name and H[qw]['mart'] == 'homeplus':
change = qw
overlap = True
if overlap == False:
soup = i.find('a', {'class': 'thumb'})
if type(soup) == type(None):
continue
imgUrl = soup.find('img')['src']
imgUrl = 'http:' + imgUrl
temp_dict['mart'] = 'homeplus'
temp_dict['name'] = name
temp_dict['price'] = price
temp_dict['unit'] = unit
temp_dict['img'] = imgUrl
H.append(temp_dict)
else:
if H[change]['unit'] > unit:
H[change]['price'] = price
H[change]['unit'] = unit
continue
print('홈플러스 물')
print(H)
print(len(H))
def home_snack(H):
page_num = 17
path = 'C:\chromedriver'
driver = webdriver.Chrome(path)
driver.get(
'http://www.homeplus.co.kr/app.exhibition.category.Category.ghs?comm=category.list&cid=60343&WT.ac=GNBctg_2dep_ctg')
for i in range(page_num - 1):
driver.find_element_by_xpath("//a[@class='more']").click()
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
table = soup.find_all('ul', {'class': 'fix-ty2'})
for five in table:
one = five.find_all('li')
for i in one:
temp_dict = {}
a = i.find('div', {'class': 'exh-comtxt w-ty4'})
if type(a) == type(None):
continue
price = a.find('span', {'class': 'price'})
unit = price.find('span', {'class': 'unit-area'})
if type(unit) == type(None):
continue
price = price.find('strong', {'class': 'buy'})
name = a.find('a', {'class': 'name'})
if type(name) == type(None):
continue
unit = unit.text
if unit.find('ML') != -1:
continue
if unit.find('개') != -1:
continue
name = name_preprocessing(name.text)
price = price.text
price = price.replace(',', '')
unit = unit_preprocessing(unit, 'G')
overlap = False
change = 0
for qw in range(len(H)):
if H[qw]['name'] == name and H[qw]['mart'] == 'homeplus':
change = qw
overlap = True
if overlap == False:
soup = i.find('a', {'class': 'thumb'})
if type(soup) == type(None):
continue
imgUrl = soup.find('img')['src']
imgUrl = 'http:' + imgUrl
temp_dict['mart'] = 'homeplus'
temp_dict['name'] = name
temp_dict['price'] = price
temp_dict['unit'] = unit
temp_dict['img'] = imgUrl
H.append(temp_dict)
else:
if H[change]['unit'] > unit:
H[change]['price'] = price
H[change]['unit'] = unit
continue
print('홈플러스 과자')
print(H)
print(len(H))
def home_coffee(H):
page_num = 2
path = 'C:\chromedriver'
driver = webdriver.Chrome(path)
driver.get(
'http://www.homeplus.co.kr/app.exhibition.category.Category.ghs?comm=category.list&cid=60406&WT.ac=GNBctg_3dep_ctg')
for i in range(page_num - 1):
driver.find_element_by_xpath("//a[@class='more']").click()
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
table = soup.find_all('ul', {'class': 'fix-ty2'})
for five in table:
one = five.find_all('li')
for i in one:
temp_dict = {}
a = i.find('div', {'class': 'exh-comtxt w-ty4'})
if type(a) == type(None):
continue
price = a.find('span', {'class': 'price'})
unit = price.find('span', {'class': 'unit-area'})
if type(unit) == type(None):
continue
price = price.find('strong', {'class': 'buy'})
name = a.find('a', {'class': 'name'})
if type(name) == type(None):
continue
unit = unit.text
if unit.find('G') != -1:
continue
name = name_preprocessing(name.text)
price = price.text
price = price.replace(',', '')
unit = unit_preprocessing(unit, 'ML')
overlap = False
change = 0
for qw in range(len(H)):
if H[qw]['name'] == name and H[qw]['mart'] == 'homeplus':
change = qw
overlap = True
if overlap == False:
soup = i.find('a', {'class': 'thumb'})
if type(soup) == type(None):
continue
imgUrl = soup.find('img')['src']
imgUrl = 'http:' + imgUrl
temp_dict['mart'] = 'homeplus'
temp_dict['name'] = name
temp_dict['price'] = price
temp_dict['unit'] = unit
temp_dict['img'] = imgUrl
H.append(temp_dict)
else:
if H[change]['unit'] > unit:
H[change]['price'] = price
H[change]['unit'] = unit
continue
print('홈플러스 커피')
print(H)
print(len(H))
H_water = {}
H_snack = {}
H_coffee = {}
#home_water(H_water)
#home_snack(H_snack)
#home_coffee(H_coffee)