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")
    ...

高级用法之一

上面配合 Saya 使用的例子展示了创建一个页面的例子,但假如我们需要一个与其他页面隔离的新页面(例如 cookie 等),那么我们可以使用 browser.page(context=True) 在创建页面时使用一个新的上下文,如下所示:

[!NOTE]
该种用法不支持持久性上下文(Persistent Context)

更多信息详见:https://playwright.dev/python/docs/browser-contexts

@listen(...)
async def function(app: Ariadne):
    browser = app.launch_manager.get_interface(PlaywrightBrowser)
    async with browser.page(new_context=True) as page:  # 此 API 启用了自动上下文管理
        await page.set_content("Hello World!")
        img = await page.screenshot(type="jpeg", quality=80, full_page=True, scale="device")
    ...

高级用法之二

上面配合 Saya 使用的例子展示了为单个页面设置 viewport 的功能,自 GraiaX Playwright v0.3.1 版本起,可以在创建 PlaywrightService 时为全局的 Browser Context 指定 viewport,然后在截图时使用全局 Browser Context 截图,如下所示:

[!NOTE]
该种用法不支持持久性上下文(Persistent Context)

机器人入口文件:

app.launch_manager.add_service(PlaywrightService("chromium"))

Saya 模块中:

from graiax.playwright import PlaywrightContext


@listen(...)
async def function(app: Ariadne):
    context = app.launch_manager.get_interface(PlaywrightContext)
    async with context.page() as page:  # 此 API 启用了自动上下文管理
        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.6.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

graiax_playwright-0.2.6-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file graiax_playwright-0.2.6.tar.gz.

File metadata

  • Download URL: graiax_playwright-0.2.6.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for graiax_playwright-0.2.6.tar.gz
Algorithm Hash digest
SHA256 bcc17de45600ab10dbab72be6094b287200c28414b576784462399e29cac6772
MD5 fdd08c4e06e1fe8d233910fffcfcc916
BLAKE2b-256 7e7e2b2c22709a876703a3ef321f3ea3e0d90f98d8a399c48c53167565a103b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for graiax_playwright-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0a2ac7675274061a74ccf4cf5cc40eb266cb1af96eaf7d210968b1476310db88
MD5 a951c7b0bb6207093ca2fd5efb19ca2b
BLAKE2b-256 d9cbd0e0b77be5ff67a9b14350091848961b6f7b4b97e06bb07b4a23c7d72d20

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