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