File "test_playwright_cf.py"

Full path: /home/algopkco/public_html/scraper/test_playwright_cf.py
File size: 999 B (999 B bytes)
MIME-type: text/x-script.python
Charset: utf-8

Download   Open   Edit   Advanced Editor &nnbsp; Back

import re
import time

from playwright.sync_api import sync_playwright

URL = "https://www.sanfoundry.com/c-plus-plus-mcqs-questions-answers/"

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    ctx = browser.new_context(
        user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36",
        viewport={"width": 1280, "height": 800},
        locale="en-US",
    )
    page = ctx.new_page()
    page.goto(URL, timeout=60000, wait_until="domcontentloaded")
    for i in range(8):
        time.sleep(4)
        title = page.title()
        print("try", i, "title:", title[:80])
        if "Just a moment" not in title and "Just a Moment" not in title:
            break
    html = page.content()
    print("final len:", len(html))
    print("contains mcq container:", "c-content" in html or "answer" in html.lower())
    open("sanfoundry_sample.html", "w", encoding="utf-8").write(html)
    browser.close()