Update README.md
This commit is contained in:
40
README.md
40
README.md
@@ -45,28 +45,36 @@ async def main(): print('hello'); return 'world'
|
|||||||
main()
|
main()
|
||||||
```
|
```
|
||||||
|
|
||||||
## playwright
|
## playwright [25-03-01]
|
||||||
```py
|
```python
|
||||||
@(sync := lambda coro: __import__('nest_asyncio').apply() or
|
def Page():
|
||||||
__import__('functools').wraps(coro)(lambda *args, **kwargs:
|
def sync(obj):
|
||||||
__import__('asyncio').run(coro(*args, **kwargs))))
|
import asyncio, functools
|
||||||
async def Page():
|
if asyncio.iscoroutine(coro := obj):
|
||||||
|
loop, future = asyncio.get_event_loop(), asyncio.ensure_future(coro)
|
||||||
|
while not future.done():
|
||||||
|
loop._process_events(loop._selector.select(0))
|
||||||
|
if (ready := loop._ready) and not (handle := ready.popleft())._cancelled:
|
||||||
|
task = (tasks := asyncio.tasks._current_tasks).pop(loop, None)
|
||||||
|
handle._run()
|
||||||
|
tasks[loop] = task
|
||||||
|
return future.result()
|
||||||
|
if asyncio.iscoroutinefunction(func := obj): return functools.wraps(func)(
|
||||||
|
lambda *args, **kwargs: sync(func(*args, **kwargs)))
|
||||||
|
for attr in dir(obj):
|
||||||
|
if asyncio.iscoroutinefunction(method := getattr(obj, attr)):
|
||||||
|
setattr(obj, attr, sync(method))
|
||||||
|
return obj
|
||||||
|
|
||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
browser = await (await async_playwright().start()).chromium.launch()
|
browser = sync(sync(async_playwright().start()).firefox.launch())
|
||||||
(context := await browser.new_context()).set_default_timeout(0)
|
page = sync(sync(browser.new_page()))
|
||||||
for attr in dir(page := await context.new_page()):
|
|
||||||
if attr[0] != '_' and callable(method := getattr(page, attr)):
|
|
||||||
setattr(page, attr, sync(method))
|
|
||||||
page._repr_png_ = page.screenshot
|
page._repr_png_ = page.screenshot
|
||||||
page.goto = lambda url, goto=page.goto: goto(url)
|
|
||||||
page.soup = lambda: __import__('bs4').BeautifulSoup(page.content(), 'lxml')
|
|
||||||
return page
|
return page
|
||||||
''
|
|
||||||
page = Page()
|
page = Page()
|
||||||
page.goto('https://naver.com')
|
page.goto('https://naver.com')
|
||||||
page
|
page
|
||||||
''
|
|
||||||
page.soup()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# wireguard
|
# wireguard
|
||||||
|
|||||||
Reference in New Issue
Block a user