-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzara.py
More file actions
executable file
·359 lines (296 loc) · 12.6 KB
/
zara.py
File metadata and controls
executable file
·359 lines (296 loc) · 12.6 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import argparse
from concurrent.futures import ProcessPoolExecutor
import time
import re
import sys
import hashlib
import os
import random
import re
import requests
import json5
import json
import pandas as pd
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from tqdm import tqdm
from torpy.http.requests import TorRequests
from email.header import Header
from wsgiref import headers
from torpy.http.requests import TorRequests
from requests.exceptions import SSLError, HTTPError
from urllib.parse import urlparse, urlunparse
from databaseClasses import PostgressDBConnection
from Scraper import Scraper
load_dotenv()
ALL_URLS_SEEN = set()
ZARA_START_URL = "https://www.zara.com/us/en/categories?ajax=true"
ZARA_BASE_URL = "https://www.zara.com/us/en/"
def find_hrefs(soup, path):
vals = [a['href'] for a in soup.find_all('a', href=True) if a['href'].startswith(path)]
return set(vals)
def setup_session(scraper):
header = scraper.get_browser_header()
session = requests.Session()
session.headers.update(header)
session.proxies.update(scraper.get_proxy())
return session
def get_categories(json_data):
category_urls = []
category_ids = []
for category in json_data.get("categories", []):
for subcategory in category.get("subcategories", []):
id = subcategory.get("id")
seo = subcategory.get("seo", {}) # Access the "seo" object
seo_category_id = seo.get("seoCategoryId")
keyword = seo.get("keyword")
category_url = f"{ZARA_START_URL}en/{keyword}-l{seo_category_id}.html?ajax=true"
category_urls.append(category_url)
category_ids.append(id)
return category_urls, category_ids
def get_urls(scraper, category_url):
session = setup_session(scraper)
response = session.get(category_url)
while response.status_code != 200:
print("Failed to get URL:", category_url, "Status Code:", response.status_code)
time.sleep(2 if response.status_code == 403 else 1)
session = setup_session(scraper)
response = session.get(category_url)
category_data = response.json()
# Extract product links from the category data and construct product links
product_links = []
for product in category_data.get("productGroups", []):
for element in product.get("elements", []):
if "commercialComponents" in element:
for component in element["commercialComponents"]:
if component["type"] == "Product":
keyword = component["seo"]["keyword"]
seo_product_id = component["seo"]["seoProductId"]
product_link = f"https://www.zara.com/us/en/{keyword}-p{seo_product_id}.html?ajax=true"
timestamp = time.time()
product_links.append([product_link, timestamp])
return product_links
def scrape_urls_zara(scraper: Scraper, url: str, base_url: str) -> list:
try:
session = setup_session(scraper)
zara_request = session.get(url)
while zara_request.status_code != 200:
print("Failed to get URL:", url, "Status Code:", zara_request.status_code)
time.sleep(2 if zara_request.status_code == 403 else 1)
session = setup_session(scraper)
zara_request = session.get(url)
print(zara_request.headers)
print(session.headers)
json_data= zara_request.json()
_, category_ids = get_categories(json_data)
all_links = []
pbar = tqdm(total=len(category_ids), desc="Scraping Zara Product Urls", leave=True)
for category_id in category_ids:
category_url = f"https://www.zara.com/us/en/category/{category_id}/products?regionGroupId=8&ajax=true"
product_links = get_urls(scraper, category_url)
all_links.extend(product_links)
pbar.update(1)
pbar.close()
print("Finished scraping Zara product urls!")
print(all_links[0])
return all_links
except HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except SSLError as ssl_err:
print(f"SSL error occurred: {ssl_err}")
except Exception as err:
print("weird error")
print(f"An error occurred: {err}")
return []
def get_price(p):
price = str(p)
if len(price) >= 2:
price = price[:-2] + "." + price[-2:]
return price
def get_material(scraper, id):
material_url = f"https://www.zara.com/us/en/product/{id}/extra-detail?ajax=true"
session = setup_session(scraper)
response = session.get(material_url)
materials = []
if response.status_code == 200:
material_info = json5.loads(response.text)
for section in material_info:
if section.get("sectionType") in ["materials", "composition"]:
for component in section.get("components"):
if "datatype" in component and component["datatype"] == "paragraph":
text = component["text"]["value"]
if "%" in text:
materials.append(text)
return ', '.join(materials)
def get_image_urls(json_data):
base_url = "https://static.zara.net/photos/"
image_urls = []
xmedia = json_data.get("xmedia", []) # Get the "xmedia" array
for img_info in xmedia:
if img_info.get("datatype") == "xmedia" and img_info.get("type") == "image":
path = img_info.get("path")
name = img_info.get("name")
timestamp = img_info.get("timestamp")
image_url = f"{base_url}{path}/w/563/{name}.jpg?ts={timestamp}"
image_urls.append(image_url)
return image_urls
def get_sizes(color):
sizes = color["sizes"]
size_availabilities = []
for size in sizes:
size_name = size["name"]
availability = size["availability"]
size_availability = f"{size_name}, {availability}"
size_availabilities.append(size_availability)
return size_availabilities
def get_gender(product_json):
gender = product_json['product']['sectionName']
gender = gender.lower()
if gender == "woman":
return "women"
elif gender == "man":
return "men"
elif gender == 'kid':
return "kid"
else:
return "unisex"
def get_product_url(product_json, i):
url = product_json['productMetaData'][i]['url']
return url
def get_product_brand(product_json, i):
brand = product_json['productMetaData'][i]['brand']
if brand == "MASSIMODUTTI":
brand = "massimo dutti"
return "zara"
def get_product_data(scraper, product_json, product_link):
product_name = product_json["product"]["name"]
colors = [color["name"] for color in product_json["product"]["detail"]["colors"]]
desc = product_json["productMetaData"][0]["description"]
material = get_material(scraper, product_json["product"]["detail"]["colors"][0]["productId"])
price = get_price(product_json["product"]["detail"]["colors"][0]["price"])
image_urls = [get_image_urls(color) for color in product_json["product"]["detail"]["colors"]]
sizes = [get_sizes(color) for color in product_json["product"]["detail"]["colors"]]
gender = get_gender(product_json)
urls = [get_product_url(product_json, i) for i in range(len(product_json['productMetaData']))]
brands = [get_product_brand(product_json, i) for i in range(len(product_json['productMetaData']))]
#Pack all of these into rows.
rows = []
time_now = time.time()
# Do this in format of mapping within the list below.
for i in range(len(colors)):
ALL_URLS_SEEN.add(urls[i])
rows.append({
"name": product_name,
"color": colors[i],
"description": desc,
"composition": material,
"price": price,
"images": image_urls[i],
"sizes": sizes[i],
"time_now": time_now,
"gender": gender,
"brand" : brands[i],
"url": urls[i],
})
return rows
def format_dictionary_data(product_data):
new_rows = []
seen_so_far = set()
for row in product_data:
if row["url"] in seen_so_far:
continue
seen_so_far.add(row["url"])
# Format images the right way first, i.e seperated by |
images = row["images"]
#Replace https with http
for i in range(len(images)):
images[i] = images[i].replace("https", "http")
images_formatted = "|".join(images)
new_rows.append([row["name"], row["gender"], row['color'], row['description'], row['composition'], row['price'], row['sizes'], images_formatted, row['url'], row['brand'], row['time_now']])
return new_rows
def scrape_items_zara(scraper: Scraper, urls: list) -> list:
"""Function that scrapes the items from the urls."""
# Create a session and get the urls
session = setup_session(scraper)
failed_urls = []
items = []
pbar = tqdm(total=len(urls), desc="Scraping Zara Products", leave=True)
count = 0
for url in urls:
if url in ALL_URLS_SEEN:
continue
time.sleep(0.2)
if count % 25 == 0 and count != 0:
# Wait extra now and then.
time.sleep(3)
if count % 50 == 0 and count != 0:
session = setup_session(scraper)
scraper.save_product(items)
items = []
try:
response = session.get(url)
fail_count = 0
while response.status_code != 200 and fail_count < 1:
print("Failed to get data:", url, "Status Code:", response.status_code, "Fail count:", fail_count)
time.sleep(2 if response.status_code == 403 else 1)
session = setup_session(scraper)
response = session.get(url)
fail_count += 1
if response.status_code != 200:
print("Final fail for url:", url, "Status Code:", response.status_code)
failed_urls.append(url)
ALL_URLS_SEEN.add(url)
continue
product_json = json5.loads(response.text)
product_data = get_product_data(scraper, product_json, url)
formatted_data = format_dictionary_data(product_data)
items.extend(formatted_data)
pbar.update(1)
count+=1
except Exception as err:
print(f"An error occurred: {err}")
failed_urls.append(url)
continue
pbar.close()
save_failed_urls(failed_urls)
return items
def save_failed_urls(failed_urls):
# Read from the file, then write to file without duplicates.
# Create file if it doenst exist.
if not os.path.exists('failed_urls_zara.txt'):
open('failed_urls_zara.txt', 'w').close()
with open('failed_urls_zara.txt', 'r') as f:
lines = f.readlines()
for line in lines:
failed_urls.append(line.strip())
failed_urls = list(set(failed_urls))
with open('failed_urls.txt', 'w') as f:
for url in failed_urls:
f.write("%s\n" % url)
def main(scrape_urls_flag, scrape_products_flag, use_residential_proxy):
if scrape_urls_flag:
# Scrape both women and men urls. For zara, this id done in one function!
scraper = Scraper(ZARA_START_URL, "zara", ZARA_BASE_URL, use_residential_proxy)
scraper.scrape_urls(lambda x, y: scrape_urls_zara(scraper, x, y))
scraper.pg.remove_url_duplicates("zara", "producturls")
scraper.kill_db_connection()
if scrape_products_flag:
# Scrape product details.
scraper = Scraper(ZARA_START_URL, "zara", ZARA_BASE_URL, use_residential_proxy)
scraper.scrape_products(lambda x: scrape_items_zara(scraper, x))
scraper.pg.remove_product_duplicates("zara", "productdata")
scraper.kill_db_connection()
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Web scraping script for H&M products.')
# Define the three arguments
parser.add_argument('--scrape-urls', action='store_true', help='Scrape URLs if set')
parser.add_argument('--scrape-products', action='store_true', help='Scrape products if set')
parser.add_argument('--residential-proxy', action='store_true', help='Use residential proxies if set')
# Parse the arguments
args = parser.parse_args()
# Write to random file to test if this works.
with open('test.txt', 'w') as f:
f.write("Hello World")
# Call the main function with the parsed arguments
main(args.scrape_urls, args.scrape_products, args.residential_proxy)