确保我的无头浏览器位于我的 Playwright + 中的正确位置蟒蛇 + AWS lambda 设置

发布于 2025-01-09 07:38:27 字数 236 浏览 1 评论 0原文

我之前曾在 JS 中使用过 playwright,因为 npm 包 (playwright-aws-lambda) 为我带来了魔力,所以生活很轻松。

我找不到与 python 类似的东西,所以我想我必须更深入地挖掘,但不知道从哪里开始。

是否有一个包支持 aws-lambda 上的 playwright+python 或为此设置添加无头浏览器的基本解决方案?

了解剧作家的替代包存在,但这不是我们在这个问题中寻找的

I have previously used playwright with JS and life was easy since an npm package (playwright-aws-lambda) did the magic for me.

I cannot find anything similar for python so I guess I have to dig deeper but do not know where to start.

Is there a package to support for playwright+python on aws-lambda or a fundamental solution to add a headless browser for this setup?

Understand that alternatives packages to playwright exists but that is not what we are looking for in this question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摘星┃星的人 2025-01-16 07:38:27

我使用 python pyppeteer lib 得到了一个解决方案。

问题是 playwright 仅适用于特定版本的 chrome。

下面的代码有效

import asyncio
from playwright.async_api import async_playwright
from flask import Response

import os
os.environ["PYPPETEER_CHROMIUM_REVISION"] = "1050513"

from pyppeteer.util import download_chromium, chromium_executable,         check_chromium


if not check_chromium():
    print(f"Baixando o navegador na versão:     
          {os.environ.get('PYPPETEER_CHROMIUM_REVISION', 'vazio')}")
    download_chromium()



args= [
   '--allow-running-insecure-content',
  '--autoplay-policy=user-gesture-required', 
  '--disable-component-update', 
  '--disable-domain-reliability',
  '--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process',
  '--disable-print-preview',
  '--disable-setuid-sandbox',
  '--disable-site-isolation-trials',
  '--disable-speech-api',
  '--disable-web-security',
  '--disk-cache-size=33554432',
  '--enable-features=SharedArrayBuffer',
  '--hide-scrollbars',
  '--ignore-gpu-blocklist',
  '--in-process-gpu',
  '--mute-audio',
  '--no-default-browser-check',
  '--no-pings',
  '--no-sandbox',
  '--no-zygote',
  '--use-gl=swiftshader',
  '--window-size=1920,1080',
]
async def run(playwright):
    path_exc = chromium_executable()
    print(path_exc)

    chromium = playwright.chromium
    browser = await chromium.launch(
        executable_path=path_exc,
        args=args,
        chromium_sandbox=False,
        handle_sigint=False,
        handle_sigterm=False,
        handle_sighup=False
        )
    context = await browser.new_context()
    page = await context.new_page()
    await page.goto("https://www.google.com")
    image = await page.screenshot()
    await browser.close()
    return image

async def main():
    async with async_playwright() as playwright:
        playwright.chromium
        return await run(playwright)

def print_tela_google(request):
    image = asyncio.run(main())
    return Response(image, mimetype='image/jpeg')

I got a solution using the python pyppeteer lib.

the problem is that playwright only works with specific versions of chrome.

code below worked

import asyncio
from playwright.async_api import async_playwright
from flask import Response

import os
os.environ["PYPPETEER_CHROMIUM_REVISION"] = "1050513"

from pyppeteer.util import download_chromium, chromium_executable,         check_chromium


if not check_chromium():
    print(f"Baixando o navegador na versão:     
          {os.environ.get('PYPPETEER_CHROMIUM_REVISION', 'vazio')}")
    download_chromium()



args= [
   '--allow-running-insecure-content',
  '--autoplay-policy=user-gesture-required', 
  '--disable-component-update', 
  '--disable-domain-reliability',
  '--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process',
  '--disable-print-preview',
  '--disable-setuid-sandbox',
  '--disable-site-isolation-trials',
  '--disable-speech-api',
  '--disable-web-security',
  '--disk-cache-size=33554432',
  '--enable-features=SharedArrayBuffer',
  '--hide-scrollbars',
  '--ignore-gpu-blocklist',
  '--in-process-gpu',
  '--mute-audio',
  '--no-default-browser-check',
  '--no-pings',
  '--no-sandbox',
  '--no-zygote',
  '--use-gl=swiftshader',
  '--window-size=1920,1080',
]
async def run(playwright):
    path_exc = chromium_executable()
    print(path_exc)

    chromium = playwright.chromium
    browser = await chromium.launch(
        executable_path=path_exc,
        args=args,
        chromium_sandbox=False,
        handle_sigint=False,
        handle_sigterm=False,
        handle_sighup=False
        )
    context = await browser.new_context()
    page = await context.new_page()
    await page.goto("https://www.google.com")
    image = await page.screenshot()
    await browser.close()
    return image

async def main():
    async with async_playwright() as playwright:
        playwright.chromium
        return await run(playwright)

def print_tela_google(request):
    image = asyncio.run(main())
    return Response(image, mimetype='image/jpeg')
合久必婚 2025-01-16 07:38:27

硒。
我不知道你的用例,但我认为硒会涵盖这一切。

如果您仍然想使用 playwright,请查看此存储库:

https://github.com/微软/playwright-python

Selenium.
I don't know about your use case but I think selenium will cover it all.

In case you still want to use playwright, have a look at this repo:

https://github.com/microsoft/playwright-python

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文