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()
|
||||
```
|
||||
|
||||
## playwright
|
||||
```py
|
||||
@(sync := lambda coro: __import__('nest_asyncio').apply() or
|
||||
__import__('functools').wraps(coro)(lambda *args, **kwargs:
|
||||
__import__('asyncio').run(coro(*args, **kwargs))))
|
||||
async def Page():
|
||||
## playwright [25-03-01]
|
||||
```python
|
||||
def Page():
|
||||
def sync(obj):
|
||||
import asyncio, functools
|
||||
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
|
||||
browser = await (await async_playwright().start()).chromium.launch()
|
||||
(context := await browser.new_context()).set_default_timeout(0)
|
||||
for attr in dir(page := await context.new_page()):
|
||||
if attr[0] != '_' and callable(method := getattr(page, attr)):
|
||||
setattr(page, attr, sync(method))
|
||||
browser = sync(sync(async_playwright().start()).firefox.launch())
|
||||
page = sync(sync(browser.new_page()))
|
||||
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
|
||||
''
|
||||
|
||||
page = Page()
|
||||
page.goto('https://naver.com')
|
||||
page
|
||||
''
|
||||
page.soup()
|
||||
```
|
||||
|
||||
# wireguard
|
||||
|
||||
Reference in New Issue
Block a user