适用于 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.1.tar.gz
.
File metadata
- Download URL: graiax-playwright-0.2.1.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dc6f14c1bb44edb52e0f002976780d5d92cab71e122ede278c1200dc0a0535d |
|
MD5 | cd92bb169f640974a2aa99a7dd0fedfe |
|
BLAKE2b-256 | ec745b33d7c4993cfd76d5543cb0cc8bb4418bf21cd10285706e0d34ea0f6046 |
File details
Details for the file graiax_playwright-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: graiax_playwright-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc76116b52dc92dbef8cefdaa3e067b6c4f70d93e6bd2e8387b2283212f8302a |
|
MD5 | 8d4c0a1896586ac3fa2dc51ff5bd7e4a |
|
BLAKE2b-256 | c4e6c23c7e1aab809d408052c557406fc16c43f8ce2de7ed888cf4691ea70263 |