Playwright 安装分两步:安装 Python 库(用 PyPI 国内源)、下载浏览器(用环境变量指定国内镜像)。下面是完整、可直接复制的命令。
一、Python 库(pip)国内源安装
1. 临时使用清华源(推荐)
bash
运行
pip install playwright -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
2. 其他常用国内源(任选其一)
bash
运行
# 阿里云pip install playwright -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com# 豆瓣pip install playwright -i https://pypi.douban.com/simple/ --trusted-host pypi.douban.com# 中科大pip install playwright -i https://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn
3. 永久设置 pip 国内源(一劳永逸)
bash
运行
# 永久设为清华源pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn# 以后直接安装即可自动走国内源pip install playwright
二、浏览器二进制文件(playwright install)国内镜像
关键:
playwright install 下载浏览器默认走国外,必须设置环境变量 PLAYWRIGHT_DOWNLOAD_HOST 指向国内镜像。Windows(PowerShell)
powershell
# 临时设置环境变量并安装所有浏览器$env:PLAYWRIGHT_DOWNLOAD_HOST="https://cdn.npmmirror.com/binaries/playwright"playwright install
Windows(CMD)
cmd
set PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright playwright install
macOS / Linux
bash
运行
# 临时export PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright playwright install# 永久(写入 ~/.bashrc 或 ~/.zshrc)echo 'export PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright' >> ~/.bashrcsource ~/.bashrc
只装某一种浏览器
bash
运行
# 只装 Chromiumplaywright install chromium# 只装 Firefoxplaywright install firefox# 只装 WebKitplaywright install webkit
三、完整一键脚本(Python + 浏览器,国内源)
Windows (PowerShell)
powershell
pip install playwright -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn$env:PLAYWRIGHT_DOWNLOAD_HOST="https://cdn.npmmirror.com/binaries/playwright"playwright install
macOS / Linux
bash
运行
pip install playwright -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cnexport PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright playwright install
四、Node.js 版 Playwright 国内源
bash
运行
# npm 淘宝镜像安装npm install -g playwright --registry=https://registry.npmmirror.com# 浏览器国内镜像# Windowsset PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright# macOS/Linuxexport PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright# 安装浏览器playwright install
五、常见问题
SSL 错误:加上
--trusted-host 域名下载仍慢 / 失败:确认环境变量已生效、网络正常、镜像可用
手动放置浏览器:下载后放到
Windows:
%USERPROFILE%\AppData\Local\ms-playwrightmacOS:
~/Library/Caches/ms-playwrightLinux:
~/.cache/ms-playwright