微博有 API 吗?用 Python 搜微博、取评论(2026)
微博开放平台的读取接口围绕授权用户,评论接口只返回授权用户的评论,未审核应用只能授权 15 个测试账号;要用程序搜公开微博、读评论,用第三方数据接口。下面用 DataXAPI 的 Python SDK 演示,免费试用,最多 333 次。 用 2 个接口:微博实时搜索 → 微博评论。下面的示例一共 12 次调用,合计 $0.0240;失败的调用不收费。
按关键词搜两页微博,取评论最多的 10 条的评论,写进一张 CSV:微博正文、转发和评论数、评论正文、评论点赞数。
第 1 步:微博实时搜索
按搜索词取一页微博,新发的在前;metrics.comments 是评论数,id 是微博编号。长微博在这里是截短的。
curl -G "https://api.dataxapi.com/v1/weibo/posts/search" \ --data-urlencode "query=新能源汽车" \ -H "Authorization: Bearer $DATAXAPI_API_KEY"
# pip install dataxapi from dataxapi import DataXAPI dx = DataXAPI() # reads DATAXAPI_API_KEY result = dx.weibo.posts.search(query="新能源汽车") print(result["data"]["items"])
// npm i @dataxapi/sdk import { DataXAPI } from "@dataxapi/sdk"; const dx = new DataXAPI(); // reads DATAXAPI_API_KEY const result = await dx.weibo.posts.search({ query: "新能源汽车" }); console.log(result.data.items);
dataxapi call weibo.posts.search 'query=新能源汽车'{
"code": "ok",
"request_id": "814a139c-cdb8-4a40-ac78-d5eea33f4a49",
"data": {
"items": [
{
"id": "5236052452850013",
"url": "https://weibo.com/6203910512/imMkNQ4Yv",
"type": "image",
"text": "周末的新能源汽车日常,随手记录一下",
"published_at": "2026-10-12T18:01:10.000Z",
"author": {
"id": "6779824410",
"name": "山野咖啡馆",
"avatar_url": "https://tvax1.sinaimg.cn/crop.0.0.512.512.180/E3e34Zyl6RyCChKcvEs7K6EZ.jpg",
"verified": false,
"followers": 2568
},
"metrics": { "likes": 0, "comments": 3, "reposts": 0 },
"images": [
"https://tvax1.sinaimg.cn/large/zhuR7jHU35sRnwVn0Z3jHZz1.jpg",
"https://tvax1.sinaimg.cn/large/OovUGuApFODFZj7FAMuTAB4S.jpg",
// … 另有 2 条
],
"hashtags": null
},
// … 另有 8 条
]
},
"page": {
"next_cursor": "c1.Zaf7tnLyz10UhVmeSCiY-wqUpL2K-Pv9eZq__BRv",
"cursor_expires_at": "2026-10-12T20:03:07.000Z",
"has_more": true,
"returned": 9
},
"warnings": [],
"meta": {
"capability": "weibo.posts.search",
"result_status": "complete",
"fetched_at": "2026-10-12T19:03:07.000Z",
"cached": false,
"replayed": false,
"unavailable": {
"title": "platform",
"lang": "capability",
"author.username": "platform",
"metrics.shares": "platform",
"metrics.favorites": "capability",
"metrics.views": "capability",
"mentions": "capability",
"is_pinned": "capability",
"post_id": "platform",
"parent_id": "platform",
"root_id": "platform"
},
"billing": {
"amount": "0.002",
"currency": "USD",
"balance_after": "9.998"
}
}
}取自 2026-10-12 19:03 UTC 录下的真实返回。名字、正文和编号是示例,链接也是,都不是录到的原文(页面不展示任何人的个人信息);数字是原样。长列表做了截断。请求号、游标和余额是示意。
微博实时搜索 · $0.0020 / 一次 · 参数、字段和报错见接口页 →
第 2 步:微博评论
把微博编号放进 id,取一页评论,约 20 条,带 IP 属地;下一页用 page.next_cursor。
curl -G "https://api.dataxapi.com/v1/weibo/posts/comments/list" \ --data-urlencode "id=5352434046339577" \ -H "Authorization: Bearer $DATAXAPI_API_KEY"
# pip install dataxapi from dataxapi import DataXAPI dx = DataXAPI() # reads DATAXAPI_API_KEY result = dx.weibo.posts.comments.list(id="5352434046339577") print(result["data"]["items"])
// npm i @dataxapi/sdk import { DataXAPI } from "@dataxapi/sdk"; const dx = new DataXAPI(); // reads DATAXAPI_API_KEY const result = await dx.weibo.posts.comments.list({ id: "5352434046339577" }); console.log(result.data.items);
dataxapi call weibo.posts.comments.list id=5352434046339577
{
"code": "ok",
"request_id": "fa38736b-0eeb-43ec-af8d-6dfed79989b4",
"data": {
"items": [
{
"id": "5253368837152553",
"text": "收藏了,周末就去试试",
"published_at": "2026-09-23T14:03:52.000Z",
"author": {
"id": "6802897337",
"name": "阿杰爱折腾",
"avatar_url": "https://tvax1.sinaimg.cn/crop.0.0.512.512.180/hFDQqZ0eykmuSoczNDhhphXJ.jpg"
},
"metrics": { "likes": 20, "comments": 11 },
"post_id": "5267001718823362",
"parent_id": null,
"root_id": null
},
// … 另有 19 条
]
},
"page": {
"next_cursor": "c1.LRIoesC7sc0iuzzCBkU3T8z207doNiGKjj2FzQDa",
"cursor_expires_at": "2026-10-12T20:01:34.000Z",
"has_more": true,
"returned": 20
},
"warnings": [],
"meta": {
"capability": "weibo.posts.comments.list",
"result_status": "complete",
"fetched_at": "2026-10-12T19:01:34.000Z",
"cached": false,
"replayed": false,
"unavailable": { "url": "platform", "author.username": "platform" },
"billing": {
"amount": "0.002",
"currency": "USD",
"balance_after": "9.998"
}
}
}取自 2026-10-12 19:01 UTC 录下的真实返回。名字、正文和编号是示例,链接也是,都不是录到的原文(页面不展示任何人的个人信息);数字是原样。长列表做了截断。请求号、游标和余额是示意。
微博评论 · $0.0020 / 一次 · 参数、字段和报错见接口页 →
示例花多少钱
两页搜索结果,再取 10 条微博各一页评论
价格是生成本页时的现行价格。失败的调用不收费。
在 AI 里做(MCP)
把 DataXAPI 接进你的 AI 客户端,只要接一次。之后直接说要什么,代理自己找到这些接口、看好价格,用你的余额调用。
{
"capability": "weibo.posts.search",
"query": "新能源汽车",
"idempotency_key": "<a new UUID>"
}用 Skill 做
为什么用 DataXAPI
- 稳定:近 30 天接口可用 99.9%,状态页公开,可以自己核对。
- 快:近 7 天付费调用的中位耗时 1.6 秒(2026-11-06 统计)。
- 省 token:微博的 11 个接口各取一份录制的真实返回,平台原始 JSON 合计 55.1 万 token,AI 通过 DataXAPI 的 MCP 读到的只有 1.7 万 token,少 97%;直接调接口拿到的 JSON 也少 94%(2026-11-06 统计)。所有平台同一种格式:作者在
author,互动数在metrics,翻页用page.next_cursor,换平台不用重写解析。 - 先看再用:每个接口的页面上都有一份录制的真实返回,字段一目了然。
- 花钱可控:失败不收费;每把 Key 能设金额上限,
X-Max-Price限定一次最多花多少,同一个Idempotency-Key24 小时内不重复扣费。 - 接 AI 方便:远程 MCP 服务已在 MCP 官方登记处登记(
com.dataxapi/mcp),Claude、Cursor、ChatGPT 都能直接用。 - 免费试用,最多 333 次,不用绑卡。
要知道的
- 要长微博全文,用
weibo.posts.get。不用微博账号和 Cookie。 - 热搜榜用
weibo.trends.list,50 个词。 - 不写代码也行:把 DataXAPI 接进 Claude 或 ChatGPT,用下面“在 AI 里做”的那句话。
完整脚本
上面这件事写成一个能直接跑的脚本:Python,用 SDK,Key 从 DATAXAPI_API_KEY 读。它写出一个 CSV 文件,最后打印调了几次、取到几条、扣了多少。
# pip install dataxapi # export DATAXAPI_API_KEY=dx_... (your key) import csv from decimal import Decimal from dataxapi import DataXAPI dx = DataXAPI() calls, fetched, charged = 0, 0, Decimal("0") def run(capability, params): """One call; counts it and what it was charged.""" global calls, charged result = dx.call(capability, params) calls += 1 charged += Decimal(str(result["meta"]["billing"]["amount"])) return result QUERY = "新能源汽车" PAGES, POSTS = 2, 10 # 1. Two pages of posts for the search terms, newest first. posts, cursor = [], None for _ in range(PAGES): page = run("weibo.posts.search", {"query": QUERY, **({"cursor": cursor} if cursor else {})}) posts += page["data"]["items"] cursor = page["page"]["next_cursor"] if not page["page"]["has_more"]: break # 2. A page of comments under the posts with the most comments. rows = [] for post in sorted(posts, key=lambda each: -(each["metrics"].get("comments") or 0))[:POSTS]: for comment in run("weibo.posts.comments.list", {"id": post["id"]})["data"]["items"]: rows.append([post.get("text"), post["metrics"].get("reposts"), post["metrics"].get("comments"), comment.get("text"), comment["metrics"].get("likes")]) fetched = len(rows) with open("weibo-comments.csv", "w", newline="", encoding="utf-8") as file: csv.writer(file).writerows([["post", "reposts", "comments", "comment", "comment_likes"], *rows]) print(f"calls: {calls}, items: {fetched}, charged: ${charged}")