zed-scrapy-playwright (zsp)
Less meta config, more Python code. 把 Scrapy 变成 Playwright 的事件调度引擎。
zsp 是一个 Scrapy 与 Playwright 的集成中间件,提供会话级控制、线性编码和精准数据传输,让你在 Scrapy 爬虫中轻松使用 Playwright 的浏览器自动化能力。
✨ 核心特性
- 会话级控制:单次
execution内支持多次page.goto(),保持页面状态 - 线性编码:告别 callback 地狱,以同步风格编写异步浏览器操作
- 精准数据传输:直接在 Page 对象中提取数据,无需额外的解析步骤
- 完整的类型提示:享受 IDE 智能补全和类型检查
- 装饰器启用模式(v0.2.0+):使用
@zsp.enable()装饰器,支持任意爬虫类 - 内置 Playwright Provider(v0.2.0+):管理浏览器生命周期,支持 CSS 选择模式
- Stealth 反检测:自动注入
playwright-stealth,降低被识别为机器人的风险
📦 安装
pip install zed-scrapy-playwright
或使用 uv(推荐):
uv add zed-scrapy-playwright
安装浏览器驱动(首次使用需要):
playwright install
🚀 快速开始
1. 启用中间件
在 settings.py 中添加:
DOWNLOADER_MIDDLEWARES = {
'zsp.handler.PlaywrightDownloaderMiddleware': 543,
}
2. 编写爬虫(使用装饰器模式)
import scrapy
import zsp
@zsp.enable()
class MySpider(scrapy.Spider):
name = "my_spider"
start_urls = ["https://example.com"]
def start_requests(self):
yield zsp.Request(
url="https://example.com",
callback=self.parse,
execution=self.execution_func,
)
async def execution_func(self, page, request):
await page.goto(request.url)
content = await page.content()
return {
"title": await page.title(),
"html": content,
}
async def parse(self, response: zsp.Response):
print("parse:", response.result)
3. 运行爬虫
scrapy crawl my_spider
📖 API 文档
zsp.enable()
装饰器,用于启用爬虫的 Playwright 支持。
@zsp.enable()
class MySpider(scrapy.Spider):
...
zsp.Request
继承自 scrapy.Request,增加以下参数:
| 参数 | 类型 | 说明 |
|---|---|---|
execution |
Callable | 异步执行函数,接收 (page, request) |
selector |
str | CSS 选择器(v0.2.0+,配合 Provider 使用) |
zsp.execution 函数签名
async def execution_func(page: Page, request: Request) -> dict | None:
# 进行浏览器操作...
return {"key": value}
- 返回
None时,默认返回HtmlResponse(页面源码)
🤝 贡献
欢迎提交 Issue 和 Pull Request!
- Fork 本仓库
- 创建你的功能分支 (
git checkout -b feature/amazing) - 提交你的更改 (
git commit -m 'Add some amazing feature') - 推送到分支 (
git push origin feature/amazing) - 打开 Pull Request
📄 许可证
本项目采用 Apache License 2.0 许可证。
版权所有 © 2026 zhouzhouos
🧩 相关链接
Release files for zed-scrapy-playwright 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| zed_scrapy_playwright-0.2.1.tar.gz | 67.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| zed_scrapy_playwright-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 138.1 kB
Release files / zed_scrapy_playwright-0.2.1.tar.gz
| Download URL | zed_scrapy_playwright-0.2.1.tar.gz |
|---|---|
| Size | 67.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e46a86165a3ba4a2726fb0e959963706e387b13c11ebaac8246b813c21f5e0cb
|
|
BLAKE2b-256 checksum How to use checksums |
348d409d06718692859e14cf4b0ab50dedb9738c691a6455db62503c1d298faf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.3","id":"zena","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / zed_scrapy_playwright-0.2.1-py3-none-any.whl
| Download URL | zed_scrapy_playwright-0.2.1-py3-none-any.whl |
|---|---|
| Size | 71.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b1f711584b5b9e0ed476721bded8643b3174f9e4151903c4abcf8bbdc6f675e
|
|
BLAKE2b-256 checksum How to use checksums |
f585a95ea79937a07b67f5030a6e86e6efc88e798e81ac9d9b0e599b89daf69f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.3","id":"zena","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|