-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht3.py
More file actions
71 lines (57 loc) · 2.45 KB
/
Copy patht3.py
File metadata and controls
71 lines (57 loc) · 2.45 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
import asyncio
from playwright.async_api import async_playwright
from jinja2 import Environment, FileSystemLoader
import os
import re
PRODUCT_URLS = [
"https://www.htgsupply.com/products/digital-greenhouse-hhp-1000w-de-grow-light/#tab-description",
# add more URLs here if needed
]
env = Environment(loader=FileSystemLoader("."))
def slugify(text):
return re.sub(r'[\W_]+', '-', text.lower()).strip("-")
async def scrape_product(page, url):
await page.goto(url, timeout=60000)
await page.wait_for_selector("h1.product-title", timeout=15000)
title = await page.locator("h1.product-title").text_content()
price = await page.locator(".price > span > bdi").first.text_content()
description_html = await page.locator("div.woocommerce-Tabs-panel--description").inner_html()
specifications = {}
spec_items = page.locator(".accordion-item")
count = await spec_items.count()
for i in range(count):
heading = await spec_items.nth(i).locator(".accordion-title").text_content()
if heading and "spec" in heading.lower():
rows = spec_items.nth(i).locator("table tr")
for j in range(await rows.count()):
th = await rows.nth(j).locator("th").text_content()
td = await rows.nth(j).locator("td").text_content()
if th and td:
specifications[th.strip()] = td.strip()
break
return {
"title": title.strip() if title else "Untitled",
"price": price.strip() if price else "N/A",
"description_html": description_html.strip() if description_html else "",
"specifications": specifications,
"url": url,
"slug": slugify(title or "product")
}
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
for url in PRODUCT_URLS:
try:
data = await scrape_product(page, url)
template = env.get_template("product_template.html")
html = template.render(**data)
output_file = f"output_{data['slug']}.html"
with open(output_file, "w") as f:
f.write(html)
print(f"[+] Wrote HTML output to: {output_file}")
except Exception as e:
print(f"[!] Error scraping {url}: {e}")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())