69 lines
2.2 KiB
Markdown
69 lines
2.2 KiB
Markdown
# IPython on Linux
|
|
```python
|
|
def run(coro): __import__('nest_asyncio').apply(); return __import__('asyncio').run(coro)
|
|
def sync(func): return lambda *args, **kwargs: run(func(*args, **kwargs))
|
|
|
|
from playwright.async_api import async_playwright
|
|
browser = run(run(async_playwright().start()).firefox.launch())
|
|
for attr in dir(page := run(browser.new_page())):
|
|
if attr[0] != '_' and callable(method := getattr(page, attr)):
|
|
setattr(page, attr, sync(method))
|
|
page._repr_png_ = page.screenshot
|
|
page.goto('https://naver.com')
|
|
```
|
|
|
|
```python
|
|
"""
|
|
Linux (WSL Debian)
|
|
|
|
pip install playwright
|
|
playwright install --with-deps
|
|
"""
|
|
def Page():
|
|
def sync(f):
|
|
import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None)
|
|
def thread():
|
|
while sync.q.get(): f,a,k = sync.p; sync.p = f(*a, **k); sync.q.task_done()
|
|
if not hasattr(sync, 'q'):
|
|
sync.q = queue.Queue(); threading.Thread(target=thread).start()
|
|
def fun(*a, **k): sync.p = f,a,k; sync.q.put(True); sync.q.join(); return sync.p
|
|
return functools.wraps(f)(fun)
|
|
@sync
|
|
def Page():
|
|
from playwright.sync_api import sync_playwright as P
|
|
(context := P().start().firefox.launch().new_context()).set_default_timeout(0)
|
|
for attr in dir(page := context.new_page()):
|
|
if attr[0] != '_' and callable(f := getattr(page, attr)):
|
|
setattr(page, attr, sync(f))
|
|
page._repr_png_ = page.screenshot
|
|
page.goto = lambda url, goto=page.goto: goto(
|
|
url if '//' in url else f'https://{url}', wait_until='networkidle')
|
|
return page
|
|
return Page()
|
|
page = Page()
|
|
page.goto('google.com')
|
|
page
|
|
```
|
|
|
|
# IPython on Windows
|
|
|
|
```python
|
|
def thread(func):
|
|
import asyncio, concurrent
|
|
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
|
return lambda: concurrent.futures.ThreadPoolExecutor().submit(func).result()
|
|
|
|
def handle(route):
|
|
print(route.request.url)
|
|
route.continue_()
|
|
|
|
@thread
|
|
def main():
|
|
from playwright.sync_api import sync_playwright
|
|
with sync_playwright() as playwright:
|
|
(page := playwright.firefox.launch().new_page()).set_default_timeout(0)
|
|
page.route('**/*', handle)
|
|
page.goto('https://google.com')
|
|
|
|
main()
|
|
``` |