⚠️ cookie 必填。关键字段:
a1(长效) +web_session(6-12h 短效) +id_token+webId/gid/acw_tc/websectiga/sec_poison_id/loadts/ets(风控指纹)。
bash
export SKILL=/path/to/xiaohongshu
python3 $SKILL/xhs-keepalive.py check # cookie 验证
# 🎯 抓特定用户的视频(⭐ 推荐 — 完全绕开 search captcha)
python3 $SKILL/xhs-harvest.py user <user_id> --limit 15 --comments 15
python3 $SKILL/xhs-harvest.py user 5c6391880000000012009893 --limit 15
# 🆘 不知道 user_id? 用 --from-name (走 search 桶,被 captcha 时会失败)
python3 $SKILL/xhs-harvest.py user --from-name "影视飓风" --limit 15 --comments 15
# 抓一个主题的热门笔记(必须 search 桶,被 captcha 时会失败)
python3 $SKILL/xhs-harvest.py hot "AI" --limit 10 --comments 10
# 已知 note_id 批量抓
python3 $SKILL/xhs-harvest.py ids id1 id2 id3 --tokens "t1,t2,t3"
xhs-harvest.py 编排工作流, xhs-fetch.py 原子操作。落盘: $SKILL/data/harvests/<topic>-<ts>/REPORT.md。
bash
xhs-harvest.py user <user_id> --limit 15 --comments 15
为什么是推荐路径:
note-item 自带 xsec_token + xsec_source=pc_user → 30/30 命中/user/profile/{uid}/{nid}?...&xsec_source=pc_user 抓详情 → 报告text
Step 1 xhs-fetch.py user <user_id> --notes 50 → user.json
↓ DOM 解析 note-item 里的 <a href>, 拿 (note_id, xsec_token, xsec_source=pc_user)
Step 2 对每条 note, 走 via-user-profile 抓详情
URL: /user/profile/{uid}/{nid}?xsec_token=...&xsec_source=pc_user
↓ 完全在 user/profile 桶, 不碰 search 桶
Step 3 落盘 + 写 REPORT.md
text
$SKILL/data/harvests/user-<uid前12位>-<时间戳>/
├── REPORT.md ← 人类可读报告
├── user.json ← 主页元数据 + 笔记列表 (带 xsec_token)
└── notes/
├── n01-*.json ← 笔记详情 + 评论
└── ...
--from-name#bash
xhs-harvest.py user --from-name "小Lin说" --limit 15
限制: 内部调 xhs-fetch.py user-search,会走 search 桶。search 桶 IP captcha 触发时此命令会失败(报"未找到匹配")。
故障时切换:
bash
# 浏览器登录 xhs → 搜索 → 进主页 → URL 末尾 32 位 hex = user_id
# 例: xhs.com/user/profile/5c6391880000000012009893
xhs-harvest.py user 5c6391880000000012009893 --limit 15 # 直接走 user/profile 桶
xhs captcha 按 URL 路径分桶,各桶独立计数:
桶路径衰减状态桶 1/search_result?keyword=... (search listing)5-10 min❌ 频繁锁桶 2/search_result/{nid}?xsec_token=...&xsec_source=pc_note5-10 min❌ 300017桶 3/user/profile/{uid} (用户主页)30+ min⚠️ 触发频率低桶 4****/user/profile/{uid}/{nid}?xsec_token=...&xsec_source=pc_user30+ min✅ 推荐路径
bash
xhs-harvest.py user <user_id> --limit 15 --comments 15
这条命令 完全不碰 search 桶,即使 search 桶被锁也照常工作。
命令失败原因xhs-fetch.py note <id> --via-search (用主页 token)30017 url invalid — token + xsec_source 不匹配xhs-fetch.py search "..." (search listing)桶 1 captchaxhs-harvest.py user --from-name "..." (search 桶被锁时)找不到匹配的 user_idxhs-harvest.py hot "..." (search 桶被锁时)拿不到候选
v1.3.0 行为变更:遇到 captcha 或 300012 IP 风控,脚本 直接报错退出,不自动 sleep + 重试。
为什么: 之前自动重试反而把 captcha 锁得更死(IP 持续被标记)。现在:
用户决定要不要重跑,什么时候重跑,要不要换 IP。
方式操作效果4G 热点手机开热点,PC 连立即换 IP,所有桶清空重启路由断电重启重新分配 IP(看 ISP)代理池脚本支持 http_proxy 环境变量批量抓推荐等衰减5-30 分钟search 桶 5-10 min,user 桶 30+ min
#坑怎么避1user-search "影视飓风" 抓到 @GOODLUCK用官方/唯一名(影视飓风 官方);Stage 1.5 自动验证 author2主页 link 没 xsec_token不会发生,30/30 都有,parse_user_note_link 拿3/explore/{id} 直接访问 300031必须带 xsec_token 走 via-user-profile 或 via-search4write_report KeyError 崩已修,全用 .get()5search 路径的 author 字段带后缀 "name2天前"``strip_author_suffix() 处理6search 关键词太泛搜不到目标笔记用全标题或前 15 字;长尾/低曝光笔记可能搜不到7笔记页 DOM 的 likes/collects 不准累计点赞以 user.json 的 likes 字段为准
场景间隔ab_open → ab_open(不同页面)必须 sleep ≥ 3s(脚本默认 4s)ab_eval → ab_open(页面切换)sleep ≥ 2sab_eval → ab_eval(同一页面)不需 sleep连续多次 ab_open(调试)必中 300012
黄金法则:能交给脚本的事,绝不要用 ab 手动打。
错误含义动作300012IP 风控(与 cookie 无关)不重试,换网络/IP300011账号异常 / cookie 失效重导 cookie → inject → check300017url is invalidxsec_token + xsec_source 不匹配;改用 via-user-profile300031笔记不可见走 via-user-profile 或 via-searchSecurity Verification(search 路径)search 桶 captcha不重试,等 5-10 min 或换 IPSecurity Verification(user/profile 路径)user 桶 captcha不重试,等 30+ min 或换 IP
经验法则:300012 → 换网络;300011 → 换 cookie;浏览器卡死 → agent-browser close --all && sleep 2 万能解。
bash
# 1. 浏览器登录 xiaohongshu.com → F12 → Application → Cookies → www.xiaohongshu.com
# 2. Console 跑 copy(document.cookie),粘到文件:
cat > $SKILL/data/cookies-raw.txt <<'EOF'
a1=xxx; web_session=xxx; id_token=xxx; ...
EOF
chmod 600 $SKILL/data/cookies-raw.txt
# 3. 转 Netscape + 验证
python3 $SKILL/xhs-keepalive.py inject # → $SKILL/data/cookies.txt
python3 $SKILL/xhs-keepalive.py check # exit 0=有效
web_session 6-12h 短效,过期报 300011 → 重导 cookies。