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

...

Ariadne.launch_blocking()

配合 Graia Saya 使用

from graia.ariadne.app import Ariadne
from graia.ariadne.util.saya import listen
from graiax.playwright import PlaywrightBrowser


@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.
        context=True, # 新建 Browser Context, 默认为 False.
        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.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for graiax-playwright-0.2.0.tar.gz
Algorithm Hash digest
SHA256 86971e329c7e0bead15b4f6fa0e8cc88c9c6bf19807c54815bedfb150845d61b
MD5 fb288d33517244d4e6b01a0633cd0b10
BLAKE2b-256 a84f51b679c6d2496150f3ea012c8e9e6433e0d328a688972c7a6b60449d4d37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for graiax_playwright-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dff568c81418037f869d3dabd29ae965c00e5f347cae5186370acdd10d385d81
MD5 22fe1b4b662a0a30d6eb0c5a79d308bb
BLAKE2b-256 755c6df297474d85516acf37e374091431baae62af2bdb23de77ad74ac6b946c

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