Update playwright/README.md
This commit is contained in:
@@ -1,24 +1,34 @@
|
|||||||
```python
|
```python
|
||||||
"""
|
"""
|
||||||
WSL Debian
|
Linux (WSL Debian)
|
||||||
|
|
||||||
pip install playwright nest_asyncio
|
pip install playwright
|
||||||
playwright install --with-deps
|
playwright install --with-deps
|
||||||
"""
|
"""
|
||||||
# %%
|
# %%
|
||||||
import asyncio; __import__('nest_asyncio').apply()
|
import asyncio, functools
|
||||||
@(sync := lambda coro: __import__('functools').wraps(coro)(
|
def _run_once(loop):
|
||||||
|
loop._process_events(loop._selector.select(0))
|
||||||
|
if (ready := loop._ready) and (handle := ready.popleft())._cancelled is False:
|
||||||
|
task = (tasks := asyncio.tasks._current_tasks).pop(loop, None)
|
||||||
|
handle._run(); tasks[loop] = task
|
||||||
|
(loop := asyncio.get_event_loop()).__class__._run_once = _run_once
|
||||||
|
asyncio.run = loop.run_until_complete; loop.__class__._check_running = lambda _: 0
|
||||||
|
|
||||||
|
@(sync := lambda coro: functools.wraps(coro)(
|
||||||
lambda *args, **kwargs: asyncio.run(coro(*args, **kwargs))))
|
lambda *args, **kwargs: asyncio.run(coro(*args, **kwargs))))
|
||||||
async def Page():
|
async def Page(headless=True):
|
||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
browser = await (await async_playwright().start()).firefox.launch()
|
browser = await (await async_playwright().start()).firefox.launch(headless=headless)
|
||||||
(page := await browser.new_page()).set_default_timeout(0)
|
(page := await browser.new_page()).set_default_timeout(0)
|
||||||
for attr in dir(page):
|
for attr in dir(page):
|
||||||
if asyncio.iscoroutinefunction(method := getattr(page, attr)):
|
if asyncio.iscoroutinefunction(method := getattr(page, attr)):
|
||||||
setattr(page, attr, sync(method))
|
setattr(page, attr, sync(method))
|
||||||
from IPython.display import Image
|
try:
|
||||||
page.goto = lambda url, goto=page.goto: goto(url) and Image(page.screenshot())
|
from IPython.display import Image
|
||||||
return page
|
page.goto = lambda url, goto=page.goto: goto(url) and Image(page.screenshot())
|
||||||
(page := Page()).goto('https://naver.com')
|
finally:
|
||||||
print(page.title()) # NAVER
|
return page
|
||||||
|
(page := Page()).goto('https://example.org')
|
||||||
|
print(page.title()) # Example Domain
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user