-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclothes_man.py
More file actions
224 lines (186 loc) · 9.81 KB
/
clothes_man.py
File metadata and controls
224 lines (186 loc) · 9.81 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
import requests
import sqlite3
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from datetime import datetime
from openpyxl import Workbook
from fake_useragent import UserAgent
from categories_man import categories_dict
# conn = sqlite3.connect('database.db')
# c = conn.cursor()
#
# data_download = """
# SELECT * FROM "categories";
# """
#
# links = c.execute(data_download)
# links_all = links.fetchall()
# categories_dict = {}
#
# for key, value in links_all:
# categories_dict[key] = []
# for k, v in links_all:
# if k == key:
# categories_dict[key].append(v)
data = datetime.now().strftime("%Y-%m-%d")
print(datetime.now())
def find_index(tag, class_, clothesBS):
index_text = clothesBS.find(tag, class_).text
index_text_list = index_text.split(' ')
index = index_text_list[-1]
return index
def find_color(tag, class_, clothesBS):
color_div = clothesBS.find(tag, class_)
color_span = color_div.find_all("span", "screen-reader-text")
color_list = []
for span in color_span:
color = span.text
color_list.append(color)
return color_list
class Clothes:
def __init__(self, path, category, clothesBS):
self.category = category
if clothesBS.find('p',
"product-detail-selected-color product-detail-color-selector__selected-color-name") != None:
index = find_index("p",
"product-detail-selected-color product-detail-color-selector__selected-color-name",
clothesBS)
else:
index = find_index("p", "product-detail-selected-color product-detail-info__color", clothesBS)
self.index = index
self.link = path
self.name = clothesBS.find('meta', property="og:title")['content']
self.description = clothesBS.find('meta', property="og:description")['content']
self.sizes = []
for span in clothesBS.find_all("span", "product-detail-size-info__main-label"):
size = span.text
self.sizes.append(size)
if category != 'promocje':
span = clothesBS.find("div", "product-detail-info__price-amount price")
price_with_currency = span.find("span", "price-current__amount").text
price_text = price_with_currency.split(' ')[0]
if len(price_text) > 6:
price_text = price_text.replace('\xa0', '')
self.price = float(price_text.replace(',', '.'))
for span in clothesBS.find_all("div", "product-detail-info__price-amount price"):
old_price_span = span.find("span", "price-old__amount price__amount price__amount-old")
if old_price_span != None:
old_price_with_currency = old_price_span.text
old_price_text = old_price_with_currency.split(' ')[0]
self.old_price = float(old_price_text.replace(',', '.'))
else:
self.old_price = None
if clothesBS.find("div",
"product-detail-color-selector product-detail-info__color-selector") != None:
self.colors = find_color("div",
"product-detail-color-selector product-detail-info__color-selector", clothesBS)
else:
color_text = clothesBS.find("p",
"product-detail-selected-color product-detail-info__color").text
color_list = color_text.split(' ')
color = color_list[0]
try:
color_int = int(color[0])
self.colors = []
except ValueError:
self.colors = [color]
else:
for span in clothesBS.find_all("div", "product-detail-info__price-amount price"):
old_price_span = span.find("span", "price-old__amount price__amount price__amount-old")
if old_price_span != None:
old_price_with_currency = old_price_span.text
old_price_text = old_price_with_currency.split(' ')[0]
self.old_price = float(old_price_text.replace(',', '.'))
span = clothesBS.find("div", "product-detail-info__price-amount price")
price_with_currency = span.find("span", "price-current__amount").text
price_text = price_with_currency.split(' ')[0]
if len(price_text) > 6:
price_text = price_text.replace('\xa0', '')
self.price = float(price_text.replace(',', '.'))
if clothesBS.find("div",
"product-detail-color-selector product-detail-info__color-selector") != None:
self.colors = find_color("div",
"product-detail-color-selector product-detail-info__color-selector",
clothesBS)
else:
color_text = clothesBS.find("p",
"product-detail-selected-color product-detail-info__color").text
color_list = color_text.split(' ')
color = color_list[0]
try:
color_int = int(color[0])
self.colors = []
except ValueError:
self.colors = [color]
else:
s = Service('C://Users/kotek/Desktop/chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get(path)
time.sleep(3)
el = driver.find_element(By.XPATH,
"//span[@class='product-detail-color-selector__color-marker']/span[@class='product-detail-color-selector__color-area']/span[@class='screen-reader-text']")
driver.execute_script("arguments[0].click();", el)
different_colour = driver.page_source
clothesBS_colour = BeautifulSoup(different_colour, features="lxml")
try:
for span in clothesBS_colour.find_all("div", "product-detail-info__price-amount price"):
old_price_with_currency = span.find("span",
"price-old__amount price__amount price__amount-old").text
old_price_text = old_price_with_currency.split(' ')[0]
self.old_price = float(old_price_text.replace(',', '.'))
except AttributeError:
pass
driver.close()
span = clothesBS_colour.find("div", "product-detail-info__price-amount price")
price_with_currency = span.find("span", "price-current__amount").text
price_text = price_with_currency.split(' ')[0]
if len(price_text) > 6:
price_text = price_text.replace('\xa0', '')
self.price = float(price_text.replace(',', '.'))
color = clothesBS_colour.find("span",
"product-detail-color-selector__color-marker product-detail-color-selector__color-marker--is-selected")
self.colors = [color.find("span", "screen-reader-text").text]
def __repr__(self):
return f'index {self.index}, link {self.link}, name {self.name}, price {self.price}, old_price {self.old_price}, description: {self.description}, colors {self.colors}, sizes {self.sizes}'
list_with_all_data = []
for category, clothes_list in categories_dict.items():
for path in clothes_list:
try:
ua = UserAgent()
headers = {'User-Agent': str(ua.chrome)}
clothes_requests = requests.get(path, headers=headers)
clothesBS = BeautifulSoup(clothes_requests.content, features="lxml")
list_with_all_data.append(Clothes(path, category, clothesBS))
except AttributeError:
print('this link no longer exists: ', path)
print('ilość linków:', len(list_with_all_data))
file_name = 'men' + data + '.xlsx'
wb = Workbook()
for key, value in categories_dict.items():
wb.create_sheet(key)
wb[key].append(
['index', 'link', 'nazwa', 'aktualna cena', 'poprzednia cena', 'opis', 'kolor', 'rozmiar'])
for clothes in list_with_all_data:
if clothes.category == key:
if len(clothes.colors) == 0:
if len(clothes.sizes) == 0:
wb[key].append([clothes.index, clothes.link, clothes.name, clothes.price, clothes.old_price,
clothes.description, None, None])
else:
for size in clothes.sizes:
wb[key].append([clothes.index, clothes.link, clothes.name, clothes.price, clothes.old_price,
clothes.description, None, size])
else:
for color in clothes.colors:
if len(clothes.sizes) == 0:
wb[key].append([clothes.index, clothes.link, clothes.name, clothes.price, clothes.old_price,
clothes.description, color, None])
for size in clothes.sizes:
wb[key].append([clothes.index, clothes.link, clothes.name, clothes.price, clothes.old_price,
clothes.description, color, size])
wb.remove(wb['Sheet'])
wb.save(file_name)
print(datetime.now())