30 lines
859 B
Python
30 lines
859 B
Python
from playwright.sync_api import sync_playwright
|
|
import time
|
|
|
|
def run_scraper():
|
|
with sync_playwright() as p:
|
|
# Launch browser (visible in headed mode)
|
|
headless = os.getenv('HEADLESS', 'true').lower() == 'true'
|
|
browser = p.chromium.launch(headless=headless)
|
|
page = browser.new_page()
|
|
|
|
print("Navigating to example.com...")
|
|
page.goto("https://example.com")
|
|
|
|
# Take a screenshot
|
|
page.screenshot(path="example.png")
|
|
print("Screenshot saved to example.png")
|
|
|
|
# Extract page title
|
|
title = page.title()
|
|
print(f"Page title: {title}")
|
|
|
|
# Demonstrate waiting and interaction
|
|
print("Waiting 2 seconds to demonstrate...")
|
|
time.sleep(2)
|
|
|
|
browser.close()
|
|
|
|
if __name__ == "__main__":
|
|
run_scraper()
|