← back to Maya Width Fix

scrape.py

31 lines

import urllib.request, re, json, time, sys

urls = [
 "island-weaves","craze","natural-elements","braided-hemp","ajiro-burst-of-happiness",
 "entwine-inlet-linen","serigraph-cityscape","beadazzled-leaf","entwine-tranquil-linen",
 "bouquet","entwine-serene-silk","wisping-weaves-montauk","beadazzled",
 "wisping-weaves-cape-may","cozy-nestle","cozy-bed-fellow","coco-chenille",
 "beadazzled-geode","hurly-burly-ii","ajiro-sunburst",
]
UA="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36"
out={}
for slug in urls:
    url=f"https://www.mayaromanoff.com/collections/{slug}"
    try:
        req=urllib.request.Request(url, headers={"User-Agent":UA})
        h=urllib.request.urlopen(req, timeout=25).read().decode("utf-8","ignore")
    except Exception as e:
        out[slug]={"width":None,"err":str(e)}; print(slug,"ERR",e); time.sleep(1); continue
    # <td...>Width</td> <td...>VALUE</td>  (case-insensitive, allow attrs/whitespace)
    m=re.search(r'>\s*Width\s*</td>\s*<td[^>]*>\s*([^<]{1,80}?)\s*</td>', h, re.I)
    val=m.group(1).strip() if m else None
    if val:
        # collapse whitespace
        val=re.sub(r'\s+',' ',val)
    out[slug]={"width":val}
    print(f"{slug:30s} -> {val}")
    time.sleep(1)
json.dump(out, open("/tmp/maya-width-fix/collection-widths.json","w"), indent=2)
print("\nWROTE /tmp/maya-width-fix/collection-widths.json")
print("clean:",sum(1 for v in out.values() if v.get('width')),"/",len(out))