Skip to main content

适用于 Graia 的 Playwright 管理器

Project description

GraiaX Playwright

适用于 Graia Project 的 Playwright 管理器

Code style: black Imports: isort License pdm-managed

Graiax Playwright 使用 launart 作为启动管理器, 适用于 AriadneAvilla

以 Ariadne 为例,通过 GraiaX Playwright 你可以轻松地在 Ariadne 启动的时候同时启动一个 Playwright,并在其退出的时候自动关闭 Playwright。

需要注意的是,Playwright 将会在运行期间保持后台常驻,
但由于并未开启任何页面,其内存占用量不是非常大(但也是可观的)。

安装

pdm add graiax-playwrightpoetry 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graiax-playwright-0.2.0.post1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

graiax_playwright-0.2.0.post1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file graiax-playwright-0.2.0.post1.tar.gz.

File metadata

File hashes

Hashes for graiax-playwright-0.2.0.post1.tar.gz
Algorithm Hash digest
SHA256 20c4e55993da5d92ade3e55bad988d8aa149d6644a8b97293df21a6670473a64
MD5 968e6741146a212d06a4c3659765c2af
BLAKE2b-256 51c449b0d702b92688de64405540e4c3d2ad47344e10f51d0b0e59d9a55d38ae

See more details on using hashes here.

File details

Details for the file graiax_playwright-0.2.0.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for graiax_playwright-0.2.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 93131aa594c6d51d68636374b7817778f982f7ac76de59fdc62a4ccec26c2672
MD5 d5467e7c72db79edaeace76cfa93d8eb
BLAKE2b-256 e6374442c9793fdc412c313af5adb8a656e50e517babfadab2edda3cb850c76f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page