-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzolando_parser.py
More file actions
83 lines (66 loc) · 2.7 KB
/
Copy pathzolando_parser.py
File metadata and controls
83 lines (66 loc) · 2.7 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
import json
from bs4 import BeautifulSoup
from playwright.sync_api import sync_playwright
URL = "https://en.zalando.de/mens-shoes/"
def get_html_with_playwright():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, slow_mo=100)
page = browser.new_page()
page.goto(URL, timeout=60000, wait_until="networkidle")
try:
page.click("button:has-text('Accept')", timeout=5000)
print("✅ Clicked Accept Cookies")
except:
print("⚠️ No cookie banner found")
for _ in range(15):
page.mouse.wheel(0, 10000)
page.wait_for_timeout(1500)
page.wait_for_timeout(3000)
page.screenshot(path="zalando.png", full_page=True)
html = page.content()
browser.close()
return html
def parse_products(html):
soup = BeautifulSoup(html, "html.parser")
products = []
for element in soup.find_all("li", class_="QjLAB7"):
try:
brand = element.find("span", class_="OBkCPz")
name = element.find("span", class_="voFjEy")
if not brand or not name:
continue
full_name = f"{brand.text.strip()} {name.text.strip()}"
a_tag = element.find("a", class_="_LM")
product_url = "https://en.zalando.de" + a_tag["href"] if a_tag else None
img_tag = element.find("img")
img_url = img_tag["src"] if img_tag else None
price_section = element.find("section")
price_spans = price_section.find_all("span") if price_section else []
current_price = price_spans[1].text.strip() if len(price_spans) > 1 else None
old_price = price_spans[3].text.strip() if len(price_spans) > 3 else None
discount = price_spans[5].text.strip() if len(price_spans) > 5 else None
products.append({
"name": full_name,
"url": product_url,
"image": img_url,
"price": current_price,
"old_price": old_price,
"discount": discount,
})
except Exception as e:
print(f"Error: {e}")
continue
return products
def save_to_json(data, filename="json/zalando_products.json"):
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def run_zalando():
print("Opening Zalando with Playwright...")
html = get_html_with_playwright()
print("Parsing products...")
products = parse_products(html)
print(f"Found {len(products)} products.")
save_to_json(products)
print("Saved to zalando_products.json")
if __name__ == "__main__":
run_zalando()