适用于 Graia 的 Playwright 管理器
Project description
Graiax Playwright 使用 launart 作为启动管理器, 适用于 Ariadne 及 Avilla。
以 Ariadne 为例,通过 GraiaX Playwright 你可以轻松地在 Ariadne 启动的时候同时启动一个 Playwright,并在其退出的时候自动关闭 Playwright。
需要注意的是,Playwright 将会在运行期间保持后台常驻,
但由于并未开启任何页面,其内存占用量不是非常大(但也是可观的)。
安装
pdm add graiax-playwright
或 poetry add graiax-playwright
。
我们强烈建议使用包管理器或虚拟环境
开始使用
以下示例以 Ariadne 为例。
机器人入口文件
from graia.ariadne.app import Ariadne
from graiax.playwright import PlaywrightService
app = Ariadne(...)
app.launch_manager.add_service(PlaywrightService("chromium")) # 默认值为 chromium
app.launch_manager.add_service(PlaywrightService(user_data_dir="./browser_data")) # 与上一行二选一,使用 Persistent Context
...
Ariadne.launch_blocking()
配合 Graia Saya 使用
from graia.ariadne.app import Ariadne
from graia.ariadne.util.saya import listen
from graiax.playwright import PlaywrightBrowser
# 此处代码为没有使用 Persistent Context 的示例
# 若使用 Persistent Context 请使用 `context = app.launch_manager.get_interface(PlaywrightContext)`
# 该方法获得的对象与 playwright.async_api.BrowserContext 兼容
@listen(...)
async def function(app: Ariadne):
browser = app.launch_manager.get_interface(PlaywrightBrowser)
# 此处的 browser 之用法与 playwright.async_api.Browser 无异,但要注意的是下方代码的返回值为 False。
# `isinstance(browser, playwright.async_api.Browser)`
async with browser.page( # 此 API 启用了自动上下文管理
viewport={"width": 800, "height": 10},
device_scale_factor=1.5,
) as page:
await page.set_content("Hello World!")
img = await page.screenshot(type="jpeg", quality=80, full_page=True, scale="device")
...
许可证
本项目使用 MIT
许可证进行许可。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file graiax-playwright-0.2.2.tar.gz
.
File metadata
- Download URL: graiax-playwright-0.2.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee0ee8468ad19a3512a2479c3e3a344e60a98926e68081ac2288f7ce7d1ad407 |
|
MD5 | 918fbda0a55fb581be32622fefc78e95 |
|
BLAKE2b-256 | 754dd1b7a42a7f65f9e110d49b2c96af791287f5a7a1db7fedbb9e8cf0545b71 |
File details
Details for the file graiax_playwright-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: graiax_playwright-0.2.2-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bd90b6cb1d891c3add1b3104201ea534ae36f77e1e939ea92b25e24a86b0263 |
|
MD5 | 0e1e9a6aedc19681786966e5635aa167 |
|
BLAKE2b-256 | 85def605874cfc986b53545cd639b60bf7924b730597da9d4c0127a4f42f65cf |