Update playwright/README.md
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
```python
|
||||
# play.py
|
||||
from playwright.async_api import async_playwright as aP
|
||||
import xvfbwrapper, io, os, asyncio
|
||||
import os, io, asyncio, xvfbwrapper
|
||||
from db import DB
|
||||
|
||||
async def Page(browser='chromium', headless=True):
|
||||
if headless: xvfbwrapper.Xvfb().start()
|
||||
else: os.environ['DISPLAY'] = ':0'
|
||||
db = DB()
|
||||
|
||||
db = DB()
|
||||
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)
|
||||
|
||||
async def save(response):
|
||||
if response.ok and not db.exists(url := response.url):
|
||||
db[url] = await response.body()
|
||||
try:
|
||||
if response.ok and not db.exists(url := response.url):
|
||||
db[url] = await response.body()
|
||||
except: pass
|
||||
|
||||
async def load(route):
|
||||
if body := db[route.request.url]:
|
||||
return await route.fulfill(body=body)
|
||||
if body := db[route.request.url]: return await route.fulfill(body=body)
|
||||
await route.continue_()
|
||||
|
||||
context.on('response', save)
|
||||
@@ -32,9 +33,8 @@ async def Page(browser='chromium', headless=True):
|
||||
|
||||
async def main():
|
||||
page = await Page()
|
||||
await page.goto('https://naver.com')
|
||||
await page.goto('https://reddit.com')
|
||||
await page.screenshot(path='screenshot.png')
|
||||
await asyncio.sleep(1) # 1초 대기 추가
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user