自动化获取 Google AI Mode、Gemini 和普通网页搜索 Cookie,通过浏览器访问页面后提取 Cookie 并提交至 Spider 服务。
get-cookies/
├── Dockerfile # 镜像构建文件
├── docker-compose.yml # Compose 编排文件
├── .env.example # 环境变量模板
├── .dockerignore
├── README.md # 本文档
└── scripts/
├── base.py # 基类:公共逻辑(浏览器启动/Cookie获取/API提交)
├── get_google_aimodel_cookie.py # Google AI 概览 Cookie 获取脚本
├── get_google_gemini_cookie.py # Google Gemini Cookie 获取脚本
├── get_google_search_cookie.py # Google 普通网页搜索 Cookie 获取脚本
├── entrypoint.sh # 容器入口
└── requirements.txt # Python 依赖
cd dockers/
docker build -t google-cookie-fetcher:latest .
docker run --rm \
--shm-size=2g \
--security-opt seccomp=unconfined \
-e COOKIE_API_HOST=http://192.168.1.100:6101 \
-e APP_AUTHORIZATION_KEY=your_key \
-e COOKIE_PLATFORM=your_platform \
google-cookie-fetcher:latest
docker run --rm \
--shm-size=2g \
--security-opt seccomp=unconfined \
-e COOKIE_API_HOST=http://192.168.1.100:6101 \
-e APP_AUTHORIZATION_KEY=your_key \
-e COOKIE_PLATFORM=your_platform \
-e COOKIE_NUM=3 \
-e COOKIE_SLEEP=15 \
-e COOKIE_PROXY=http://user:pass@proxy-host:port \
google-cookie-fetcher:latest
docker run -d \
--name cookie-fetcher \
--shm-size=2g \
--security-opt seccomp=unconfined \
--restart unless-stopped \
-e COOKIE_API_HOST=http://192.168.1.100:6101 \
-e APP_AUTHORIZATION_KEY=your_key \
-e COOKIE_NUM=2 \
-e COOKIE_CRON="0 */4 * * *" \
-e COOKIE_PLATFORM=your_platform \
google-cookie-fetcher:latest
0 */4 * * *= 每 4 小时执行一次
# 1. 配置环境变量
cp .env.example .env
vim .env
# 2. 启动
docker compose up -d
# 3. 查看日志
docker compose logs -f cookie-fetcher
| 变量 | 必填 | 默认值 | 说明 |
|---|---|---|---|
COOKIE_API_HOST | ✅ | http://127.0.0.1:6101 | Spider API 服务地址 |
APP_AUTHORIZATION_KEY | ✅ | - | API 鉴权密钥 |
COOKIE_API_PATH | ❌ | /api/cookie/add | Cookie 提交接口路径,与 COOKIE_API_HOST 拼接 |
COOKIE_NUM | ❌ | 1 | 每次执行获取 Cookie 的次数 |
COOKIE_SLEEP | ❌ | 10 | 每次获取间隔秒数 |
COOKIE_EXPIRE_HOURS | ❌ | 24 | Cookie 有效期,单位为正整数小时,从每次提交时开始计算 |
COOKIE_PROXY | ❌ | - | HTTP 代理,格式: http://user:pass@host:port |
COOKIE_CRON | ❌ | - | cron 表达式,留空则单次执行后退出 |
COOKIE_PLATFORM | ✅ | google_aimode | 平台选择: google_aimode (默认)、gemini(或 google_gemini)、google_search |
在 .env 或容器环境变量中设置 COOKIE_PLATFORM=google_search,使用 docker compose up -d --build 构建并启动。也可直接执行:
python scripts/get_google_search_cookie.py --api-host http://127.0.0.1:6101 --api-path /api/cookie/add
脚本直接访问 https://www.google.com/search?hl=en-US&q=123,等待搜索结果标题出现后提取 Cookie,以 platform=6 提交。保存的 base_url 会将查询参数 q 替换为 {query},例如 https://www.google.com/search?hl=en-US&q={query};form_params 保存请求头和空请求体。检测到验证码或 Google 验证跳转时,本次采集失败。批量、代理、定时运行及自定义提交路径均复用公共配置。
无需启动浏览器或访问外部 API 的回归检查(在本目录运行,需先安装 scripts/requirements.txt):
python -m unittest discover -s tests -p '*_test.py' -v
所有平台的 Cookie 默认有效期为 24 小时。可在 .env 或容器环境变量中设置 COOKIE_EXPIRE_HOURS=12,改为 12 小时。直接运行脚本时使用:
python scripts/get_google_search_cookie.py --expire-hours 12
命令行参数优先于环境变量,未配置或环境变量为空时使用 24 小时。有效期必须为正整数小时;单次、批量和定时模式均适用,每条 Cookie 提交时重新计算 expires_at。此设置控制提交到 API 的过期时间,不会延长 Google 自身 Cookie 的实际有效期。
脚本获取 Cookie 后会调用 POST {COOKIE_API_HOST}/{COOKIE_API_PATH} 接口提交,拼接时会处理地址末尾和路径开头的 /,默认路径为 /api/cookie/add。也可手动调用该接口。
如需提交到其他路径,在 .env 或容器环境变量中配置:
COOKIE_API_HOST=https://spider.example.com
COOKIE_API_PATH=/custom/cookie/create
上述配置会提交到 https://spider.example.com/custom/cookie/create。直接运行 Python 脚本时,可使用 --api-path /custom/cookie/create,命令行参数优先于环境变量;未配置或配置为空时使用默认路径。所有平台和定时模式均支持此配置。
POST {COOKIE_API_HOST}/{COOKIE_API_PATH}
Content-Type: application/json
Authorization: Bearer {APP_AUTHORIZATION_KEY}
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
platform | int | ✅ | 平台 ID:2=Google AI Mode,3=Google Gemini,6=Google Search |
cookie | string | ✅ | Cookie 字符串,格式 k1=v1; k2=v2; ... |
base_url | string | ✅ | 请求基础地址(含 {query} 占位符) |
form_params | string | ❌ | JSON 字符串,含 headers 和 form_params |
expires_at | string | ❌ | 过期时间,格式 YYYY-MM-DD HH:MM:SS,默认 1 天后 |
# 提交 Google AI 概览 Cookie(platform=2)
curl -X POST http://127.0.0.1:6101/api/cookie/add \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_auth_key" \
-d '{
"platform": 2,
"cookie": "__Secure-1PSID=xxx; __Secure-1PSIDTS=yyy; NID=zzz",
"base_url": "https://www.google.com/async/folif?q={query}&...",
"form_params": "{\"form_params\": \"\", \"headers\": {}}",
"expires_at": "2026-07-29 18:00:00"
}'
# 提交 Google Gemini Cookie(platform=3,含 form_params)
curl -X POST http://127.0.0.1:6101/api/cookie/add \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_auth_key" \
-d '{
"platform": 3,
"cookie": "__Secure-1PSID=xxx; HSID=yyy; SSID=zzz",
"base_url": "https://gemini.google.com/_/BardChat/data/assistant.lamda.StreamGenerate",
"form_params": "{\"headers\": {\"content-type\": \"application/x-www-form-urlencoded;charset=UTF-8\"}, \"form_params\": \"f.req=%5B...%5D&q={query}\"}",
"expires_at": "2026-07-29 18:00:00"
}'
import requests
resp = requests.post(
"http://127.0.0.1:6101/api/cookie/add",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer your_auth_key",
},
json={
"platform": 2,
"cookie": "__Secure-1PSID=xxx; NID=yyy",
"base_url": "https://www.google.com/async/folif?q={query}&...",
"form_params": '{"form_params": "", "headers": {}}',
"expires_at": "2026-07-29 18:00:00",
},
timeout=10,
)
data = resp.json()
print(data)
# 成功: {"code": 200, "msg": "ok", ...}
# 失败: {"code": 400, "msg": "cookie 已存在", ...}
| 字段 | 类型 | 说明 |
|---|---|---|
code | int | 200=成功,其他=失败 |
msg | string | 结果描述 |
// 成功
{"code": 200, "msg": "ok"}
// 失败(Cookie 为空)
{"code": 400, "msg": "cookie 不能为空"}
// 失败(鉴权失败)
{"code": 401, "msg": "unauthorized"}
# 导出
docker save google-cookie-fetcher:latest | gzip > google-cookie-fetcher.tar.gz
# 目标服务器导入
docker load < google-cookie-fetcher.tar.gz
# 打标签
docker tag google-cookie-fetcher:latest your-registry.com/google-cookie-fetcher:latest
# 推送
docker push your-registry.com/google-cookie-fetcher:latest
# 目标服务器拉取
docker pull your-registry.com/google-cookie-fetcher:latest
| 表达式 | 含义 |
|---|---|
0 */2 * * * | 每 2 小时 |
0 */4 * * * | 每 4 小时 |
0 */6 * * * | 每 6 小时 |
0 0 * * * | 每天凌晨 |
0 8,20 * * * | 每天 8:00 和 20:00 |
*/30 * * * * | 每 30 分钟 |
--shm-size=2g--security-opt seccomp=unconfinedhost.docker.internal 访问Content type
Image
Digest
sha256:1ac964def…
Size
425.3 MB
Last updated
17 days ago
docker pull cloty/google-cookie-fetcher:1.0.1