From 6853e6e8672acb05a135e925a429dddbdd02716f Mon Sep 17 00:00:00 2001 From: jay817 Date: Fri, 23 Aug 2024 08:28:03 -0400 Subject: [PATCH] Update playwright/README.md --- playwright/README.md | 47 ++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/playwright/README.md b/playwright/README.md index 55fca87..ca1c28e 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -1,28 +1,29 @@ ```python -# pip install playwright && playwright install --with-deps chromium +from playwright.async_api import async_playwright as aP +from db import DB +import xvfbwrapper, io, PIL.Image, os -def GET(url): - html = __import__('queue').Queue() - def tGET(): - async def aGET(): - from playwright.async_api import async_playwright - async with async_playwright() as playwright: - browser = await playwright.chromium.launch() - page = await browser.new_page() - html.put(await page.goto(url) and await page.content()) - __import__('asyncio').run(aGET()) - __import__('threading').Thread(target=tGET).start() - return html.get() +async def Page(browser='chromium', headless=True): + if headless: xvfbwrapper.Xvfb().start() + else: os.environ['DISPLAY'] = ':0' + db = DB() -# 사용 예 -html = GET('https://www.freeproxy.world/') -print(html) + playwright = await aP().start() + browser = await getattr(playwright, browser).launch(headless=False) + context = await browser.new_context(accept_downloads=True) + context.set_default_timeout(0) -# Output: -""" - - - -FreeProxy.World - Free Proxy List -""" + async def handle_request(route): + url = route.request.url + if body := db[url]: return await route.fulfill(body=body) + if response := await route.continue_(): + if response.ok and not db.exists(url): + db[url] = await response.body() + await route.fulfill(response=response) + + await context.route('**/*', handle_request) + for block in ['**/*.gif', '**/css*.js']: + await context.route(block, lambda route: route.abort()) + + return await context.new_page() ``` \ No newline at end of file