"""Open a web page in a hidden browser, grab its title and main text, save a Markdown summary. Run it from your project folder: python3 summarize_page.py Needs Playwright (python3 -m pip install playwright) and its browser (python3 -m playwright install chromium). """ from datetime import datetime from pathlib import Path from playwright.sync_api import sync_playwright # Change this to any public page once the example works. URL = "https://example.com" # The summary is written next to this script. OUTPUT = Path(__file__).parent / "summary.md" with sync_playwright() as p: browser = p.chromium.launch(headless=True) # headless = no visible window page = browser.new_page() page.goto(URL, wait_until="domcontentloaded", timeout=30_000) title = page.title() # Prefer the page's
element, fall back to if it has none. main = page.locator("main") text = (main.first if main.count() else page.locator("body")).inner_text() browser.close() lines = [line.strip() for line in text.splitlines() if line.strip()] OUTPUT.write_text( f"# {title}\n\n" f"- **Source:** {URL}\n" f"- **Fetched:** {datetime.now():%Y-%m-%d %H:%M}\n" f"- **Words:** {len(text.split())}\n\n" "## Main text\n\n" + "\n\n".join(lines) + "\n" ) print(f"Wrote {OUTPUT} ({title!r})")