内网离线安装playwright

参考

https://zhuanlan.zhihu.com/p/1896980264669909081

步骤

  1. 根据报错,下载版本匹配的 chromium-win64.zip chromium-headless-shell-win64.zip winldd-win64.zip (这个链接是2025-8月的1187版本for windows x86_64)
  2. pip install mitmproxy
  3. 复制下面脚本为redirect.py,修改REMAP变量为代理地址:本地路径
redirect.py
#!/bin/env mitmdump -s redirect.py -p 8080 --mode upstream:http://localhost:10808
"""
TODO: currently you need to install manually mitmproxy .pem certificate into system trust store
"""""
from mitmproxy import http, ctx
from mitmproxy.http import Response

Log = ctx.log
REMAP = {
    "http://127.0.0.1:8080/builds/chromium/1187/chromium-win64.zip": r"~/Downloads/chromium-win64.zip",
    "http://127.0.0.1:8080/builds/chromium/1187/chromium-headless-shell-win64.zip": r"~/Downloads/chromium-headless-shell-win64.zip",
    "http://127.0.0.1:8080/builds/chromium/1007/winldd-win64.zip": r"~/Downloads/winldd-win64.zip"
}

def request(flow: http.HTTPFlow) -> None:
    file = REMAP.get(flow.request.url, None)
    if file:
        Log.info(f"[Remap ? →裟]\t{flow.request.url=}")
        try:
            # 读取本地文件内容
            with open(file, "rb") as f:
                content = f.read()
            if len(content) == 0:
                Log.warn(f"Local {file=} is empty.")
                # 可选操作:返回空响应
                flow.response = Response.make(
                    204, b"", {"Content-Type": "text/plain"}  # 无内容
                )
                return

            # 构建响应
            flow.response = Response.make(
                200,  # 状态码
                content,  # 响应内容
                {"Content-Type": "application/octet-stream"},  # 响应头
            )
            Log.info(f"' {file=}")
        except Exception as e:
            Log.warn(e)
            # 可选操作:返回错误响应
            flow.response = Response.make(
                500,  # 内部服务器错误
                b"Failed to load local file",
                {"Content-Type": "text/plain"},
            )
    else:
        Log.info(f"[Direct]\t{flow.request.url=}")
  1. mitmweb -s redirct.py -p 10086(可将mitmweb替换为mitmdump
  2. 修改playwright的安装脚本,将https://cdn.playwright.dev/dbazure/download/playwright替换为http://127.0.0.1:8080。版本号在node_modules/playwright-core/browsers.json查询。
    • 若是npm安装的js版本playwright,则其位置在项目目录/node_modules/playwright-core/lib/server/registry/index.js
    • 若是pip安装的pypi包,自己找一下,全局搜索就能出来(😜知道的小伙伴在评论区告诉我一下)
  3. bun x playwright install(bun可替换为pnpm)重新触发playwright的安装脚本,OK!

起因

slidv 导出需要 playwright

posted @ 2025-12-08 09:34  Nolca  阅读(3)  评论(0)    收藏  举报