FeedMCP

技术指南

用 Python 调用小红书 API:搜索笔记并翻页导出评论到 CSV

本指南演示如何用不到 40 行 Python 代码调用 FeedMCP 小红书接口:先按关键词搜笔记,再按 page.next_cursor 翻页抓取评论并导出为 CSV,全程带 X-Max-Price 控价和失败不扣费保障。

涉及的接口与价格

接口 路径 单价
小红书笔记搜索 GET /v1/xiaohongshu/notes/search $0.02
小红书笔记详情 GET /v1/xiaohongshu/notes/get $0.02
小红书笔记评论 GET /v1/xiaohongshu/notes/comments/list $0.02
小红书评论的回复 GET /v1/xiaohongshu/notes/comments/replies/list $0.02

实现步骤

1. 获取 API Key 并设置环境变量

在控制台注册拿 $0.10 免费额度并建一把 Key,执行 export FEEDMCP_API_KEY="fmk_live_…"。

2. 搜索目标笔记并拿到 note_id

调用 GET /v1/xiaohongshu/notes/search($0.02/次)拿到按互动排序的笔记列表。

3. 按游标翻页抓评论并写入 CSV

循环调用 GET /v1/xiaohongshu/notes/comments/list($0.02/页),把 page.next_cursor 传给下一页直到满足页数上限。

完整代码示例

import csv, os, uuid, requests

API = "https://api.feedmcp.com"
HEADERS = {
    "Authorization": f"Bearer {os.environ['FEEDMCP_API_KEY']}",
    "X-Max-Price": "0.02",
}

def search_notes(query: str):
    r = requests.get(f"{API}/v1/xiaohongshu/notes/search", params={"query": query}, headers=HEADERS, timeout=30)
    r.raise_for_status()
    return r.json()["data"]["items"]

def export_comments(note_id: str, max_pages: int = 3, out_csv: str = "xhs_comments.csv"):
    cursor, rows = None, []
    for _ in range(max_pages):
        params = {"id": note_id, **({"cursor": cursor} if cursor else {})}
        headers = {**HEADERS, "Idempotency-Key": str(uuid.uuid4())}
        body = requests.get(f"{API}/v1/xiaohongshu/notes/comments/list", params=params, headers=headers, timeout=30).json()
        if body.get("code") != "ok":
            break
        rows.extend(body["data"]["items"])
        cursor = (body.get("page") or {}).get("next_cursor")
        if not cursor:
            break
    with open(out_csv, "w", newline="", encoding="utf-8-sig") as f:
        w = csv.writer(f)
        w.writerow(["id", "author", "likes", "created_at", "text"])
        for c in rows:
            w.writerow([c["id"], (c.get("author") or {}).get("name"), c.get("likes"), c.get("created_at"), c.get("text")])
    print(f"Saved {len(rows)} comments to {out_csv}")

if __name__ == "__main__":
    notes = search_notes("camping tent")
    if notes:
        export_comments(notes[0]["id"], max_pages=3)

费用估算

上述脚本运行一次(1 次搜索 + 3 页评论)共 4 次调用,总计 $0.08,使用新用户免费送的 $0.10 额度即可完整跑通。

常见问题

跑完这份教程大概要花多少钱?

上述脚本运行一次(1 次搜索 + 3 页评论)共 4 次调用,总计 $0.08,使用新用户免费送的 $0.10 额度即可完整跑通。

没有代码基础,能在 AI 里直接完成同样的事吗?

可以。把 FeedMCP 的 MCP 服务装进 Claude、Cursor 或 Cherry Studio 后,用自然语言即可完成同样的查询与汇总。

更多指南