Update playwright/README.md
This commit is contained in:
@@ -1,28 +1,29 @@
|
|||||||
```python
|
```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):
|
async def Page(browser='chromium', headless=True):
|
||||||
html = __import__('queue').Queue()
|
if headless: xvfbwrapper.Xvfb().start()
|
||||||
def tGET():
|
else: os.environ['DISPLAY'] = ':0'
|
||||||
async def aGET():
|
db = DB()
|
||||||
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()
|
|
||||||
|
|
||||||
# 사용 예
|
playwright = await aP().start()
|
||||||
html = GET('https://www.freeproxy.world/')
|
browser = await getattr(playwright, browser).launch(headless=False)
|
||||||
print(html)
|
context = await browser.new_context(accept_downloads=True)
|
||||||
|
context.set_default_timeout(0)
|
||||||
|
|
||||||
# Output:
|
async def handle_request(route):
|
||||||
"""
|
url = route.request.url
|
||||||
<!DOCTYPE html><html lang="en"><head>
|
if body := db[url]: return await route.fulfill(body=body)
|
||||||
<meta charset="utf-8">
|
if response := await route.continue_():
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
|
if response.ok and not db.exists(url):
|
||||||
<title>FreeProxy.World - Free Proxy List</title>
|
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()
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user