Skip to main content

适用于 Graia 的 Playwright 管理器

Project description

GraiaX Playwright

适用于 Graia Project 的 Playwright 管理器

Code style: black Imports: isort License pdm-managed PyPI

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.4.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

graiax_playwright-0.2.4-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file graiax-playwright-0.2.4.tar.gz.

File metadata

  • Download URL: graiax-playwright-0.2.4.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for graiax-playwright-0.2.4.tar.gz
Algorithm Hash digest
SHA256 bb7fc69a0e6daecd3c70e9a3dd909c61ae97d8eb4eed9920679881a6dfe21f17
MD5 5db27352b590abd360447a8bd7ee50cb
BLAKE2b-256 035ced900bd5e6fd2536a44496f340ce283c9e1ccd6c89c6be0e7e61460708a8

See more details on using hashes here.

File details

Details for the file graiax_playwright-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for graiax_playwright-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8a2f5620a8f518451ae6d860199aeb8a30e67bf128c09cfc9b4f29119ae45cc7
MD5 585b1f6a918778589d45053150be612e
BLAKE2b-256 11378a3e33c96b2cb28e888393752c92bb00579348328b61e89ee86dfc820df7

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