#!/usr/bin/env python3 """ Captura screenshots de alojadamaria.com para auditoria visual SEO/UX """ from playwright.sync_api import sync_playwright import json import time import os BASE_URL = "https://alojadamaria.com/" OUTPUT_DIR = "/media/ealmeida/Dados/Hub/03-Propostas/ALojaDaMaria/screenshots/alojadamaria" os.makedirs(OUTPUT_DIR, exist_ok=True) VIEWPORTS = { "desktop": {"width": 1440, "height": 900}, "mobile": {"width": 375, "height": 812}, } PAGES = { "homepage": BASE_URL, "categoria": BASE_URL + "product-category/novidades/", "contacto": BASE_URL + "contactos/", } def capturar(page, url, nome, viewport): """Captura acima da dobra e página completa""" print(f" -> A capturar: {nome} ({viewport['width']}x{viewport['height']})") try: page.goto(url, wait_until="networkidle", timeout=30000) time.sleep(2) # Fechar pop-ups comuns (cookie consent, newsletter) for selector in [ "button[class*='close']", "button[class*='dismiss']", "[class*='cookie'] button", "[id*='cookie'] button", "[class*='popup-close']", ".pum-close", "button[aria-label*='Close']", "button[aria-label*='close']", ]: try: el = page.query_selector(selector) if el and el.is_visible(): el.click() time.sleep(0.5) except Exception: pass # Above the fold (viewport apenas) page.screenshot( path=f"{OUTPUT_DIR}/{nome}_atf.png", full_page=False, clip={"x": 0, "y": 0, "width": viewport["width"], "height": viewport["height"]}, ) # Página completa page.screenshot( path=f"{OUTPUT_DIR}/{nome}_full.png", full_page=True, ) # Recolher metadados title = page.title() h1_els = page.query_selector_all("h1") h1_texts = [el.inner_text().strip() for el in h1_els if el.is_visible()] nav_visible = bool(page.query_selector("nav, [class*='nav'], [class*='menu']")) ctas = [] for sel in ["a[class*='btn'], a[class*='button'], button[class*='btn'], .add-to-cart, [class*='cta']"]: els = page.query_selector_all(sel) for el in els[:5]: try: if el.is_visible(): ctas.append(el.inner_text().strip()[:50]) except Exception: pass popup_visible = bool(page.query_selector(".pum-overlay, [class*='popup'][style*='display: block'], [class*='modal'][style*='display: block']")) # Dimensões do logo logo = page.query_selector("img[class*='logo'], a[class*='logo'] img, header img, .site-logo img") logo_info = None if logo: try: bb = logo.bounding_box() logo_info = bb except Exception: pass return { "url": url, "title": title, "h1": h1_texts, "nav_visible": nav_visible, "ctas_sample": ctas[:8], "popup_detected": popup_visible, "logo_bounding_box": logo_info, } except Exception as e: print(f" ERRO: {e}") return {"error": str(e)} def main(): resultados = {} with sync_playwright() as p: browser = p.chromium.launch(headless=True) for device_name, viewport in VIEWPORTS.items(): print(f"\n[{device_name.upper()}] {viewport['width']}x{viewport['height']}") context = browser.new_context( viewport=viewport, user_agent="Mozilla/5.0 (compatible; AuditBot/1.0)", locale="pt-PT", ) page = context.new_page() for page_name, url in PAGES.items(): chave = f"{device_name}_{page_name}" print(f" Página: {page_name}") dados = capturar(page, url, chave, viewport) resultados[chave] = dados context.close() browser.close() with open(f"{OUTPUT_DIR}/metadados.json", "w", encoding="utf-8") as f: json.dump(resultados, f, ensure_ascii=False, indent=2) print("\nCaptura concluída. Ficheiros em:", OUTPUT_DIR) return resultados if __name__ == "__main__": main()