Twitter 主页抓取 API
最后更新: · REST · JSON
发送 GET https://proxy.scrapier.io/api/v2/twitter-tweets/?usernames=elonmusk,并在 X-API-Key 请求头中带上你的密钥,即可以 JSON 格式获得一页推文。每页推文 10 积分;失败的调用免费。
- 每页推文 10 积分
- 失败的调用免费
- 每个密钥 3 个并行请求
调用 Twitter 主页抓取 API
在你自己的应用、脚本或智能体中使用 Twitter 主页抓取 API。你需要一个 Scrapier 账号,以及控制台里的 API 密钥。请选择一种语言。
import requests# Replace YOUR_API_KEY with the key from your Scrapier dashboardAPI_KEY = "YOUR_API_KEY"params = { "usernames": "elonmusk", "with_replies": "true", "include_user_info": "true",}r = requests.get( "https://proxy.scrapier.io/api/v2/twitter-tweets/", headers={"X-API-Key": API_KEY}, params=params, timeout=210,)r.raise_for_status()# The result sits under data.tweetsresult = r.json()["data"]["tweets"]print(result)
// Replace YOUR_API_KEY with the key from your Scrapier dashboardconst API_KEY = "YOUR_API_KEY";const params = new URLSearchParams({ usernames: "elonmusk", with_replies: "true", include_user_info: "true",});const res = await fetch(`https://proxy.scrapier.io/api/v2/twitter-tweets/?${params}`, { headers: { "X-API-Key": API_KEY }, signal: AbortSignal.timeout(210_000),});if (!res.ok) throw new Error(`HTTP ${res.status}`);const { data } = await res.json();console.log(data.tweets);
# One GET request. The key goes in a header, never in the URL.GET /api/v2/twitter-tweets/?usernames=elonmusk&with_replies=true&include_user_info=true HTTP/1.1Host: proxy.scrapier.ioX-API-Key: YOUR_API_KEYAccept: application/json
# Scrapier publishes an OpenAPI spec that covers every endpoint. No API key needed.curl -O https://proxy.scrapier.io/api/v2/openapi.json# Import openapi.json into Postman, Insomnia, Bruno or a code generator,# then add your key as the X-API-Key header.
# 1. Add the Scrapier MCP server to Claude Codeclaude mcp add -t http scrapier https://mcp.scrapier.io/mcp --header "X-API-Key: YOUR_API_KEY"# 2. Ask your agent. It finds the endpoint with search_endpoints# and calls it with run_scrape:"Get the latest tweets from the X profile @elonmusk"run_scrape("twitter-tweets", {usernames: "elonmusk"})
Python 版 Twitter 主页抓取 API
只需普通的 requests,无需 SDK。每个响应都是类型化 JSON。
安装
JavaScript 版 Twitter 主页抓取 API
可在 Node 18+、Deno、Bun 和边缘运行时中使用内置的 fetch。
检查你的环境
HTTP 版 Twitter 主页抓取 API
一次 GET 请求。认证只需一个 X-API-Key 请求头。
检查你的环境
Twitter 主页抓取 API 的 OpenAPI 定义
下载一次规范,然后导入到你已在使用的工具中。获取它无需密钥。
下载规范
通过 MCP 使用 Twitter 主页抓取 API
同一个端点也是 Claude Code、Cursor 和 Windsurf 的工具调用。用户名相同,每页推文同样是 10 积分。
安装
其他调用方式
Twitter 主页抓取 API 交互式试用区
编辑请求,观察代码更新,然后运行。没有密钥时它显示目录示例,不计费。使用你的密钥时,它会从浏览器发出真实调用;密钥只会发送到 proxy.scrapier.io,从不存储。
GET /api/v2/twitter-tweets/?usernames=elonmusk点击运行请求发送。没有密钥时你得到的是目录示例。
失败的调用不计费。
无论输入什么,示例都相同:它展示的是响应结构,而不是你输入的账户。
示例响应,目录示例,2026 年11月 9 日
{
"success": true,
"data": {
"usernames": "BillGates",
"user_id": "50393960",
"screen_name": "BillGates",
"page": 1,
"page_size": 20,
"tweets": [
{
"id": "1948167071684231344",
"tweet_id": "1948167071684231344",
"url": "https://x.com/BillGates/status/1948167071684231344",
"verified": true,
"username": "@BillGates",
"fullname": "Bill Gates",
"timestamp": "Wed Jul 23 23:43:19 +0000 2025",
"text": "Learning how many kids were dying from diarrhea back in 1997 ... https://t.co/NYitqiRY38",
"links": [
"https://gatesnot.es/46lTfBz"
],
"isPinned": false,
"isQuote": false,
"isRetweet": false,
"isReply": false,
"likes": 5219,
"replies": 0,
"retweets": 1816,
"quotes": 598,
"bookmarks": 334,
"views": 2828080,
"lang": "en",
"user": {
"avatar": "https://pbs.twimg.com/profile_images/1879013694367252480/Gxa-Pspq_normal.jpg",
"username": "BillGates",
"userFullName": "Bill Gates",
"description": "Sharing things I'm learning through my foundation work and other interests.",
"location": "Seattle, WA",
"website": "https://gatesnot.es/tgn",
"joinDate": "Wed Jun 24 18:44:10 +0000 2009",
"verified": true,
"totalLikes": 542,
"totalTweets": 4521,
"totalFollowing": 571,
"totalFollowers": 65231300,
"url": "https://t.co/TiXXwOGE95",
"userId": "50393960",
"totalMediaCount": 1530
},
"media": [],
"tweetUserId": "50393960",
"input_username": "BillGates",
"scraped_at": "2026-10-03T03:35:53.722Z"
}
],
"next_cursor": "eyJoIjoiLi4uIiwiaW4iOiJCaWxsR2F0ZXMiLCJsdCI6IjE5MjYwNDY2MzQ0NDc1NTg1NjYiLCJwIjoyLCJyIjp0cnVlLCJzbiI6IkJpbGxHYXRlcyIsInUiOiI1MDM5Mzk2MCIsInVpIjp0cnVlfQ"
}
}你可以提取的数据
Twitter 主页抓取器根据用户名、数字用户 ID 或主页 URL 提取某个 X 主页的推文。下面每个字段都来自目录中的示例响应,以带类型的 JSON 呈现。
推文
响应中有 8 个字段。
- id
- tweet_id
- url
- text
- timestamp
- lang
- links
- media
互动
响应中有 6 个字段。
- likes
- replies
- retweets
- quotes
- bookmarks
- views
类型标记
响应中有 4 个字段。
- isPinned
- isQuote
- isRetweet
- isReply
作者
响应中有 5 个字段。
- username
- fullname
- verified
- tweetUserId
- user
输入与时间戳
响应中有 2 个字段。
- input_username
- scraped_at
响应结构与分页
响应中有 7 个字段。
- success
- data.usernames
- data.user_id
- data.screen_name
- data.page
- data.page_size
- data.next_cursor
响应结构参考
示例响应中的每个字段。字段名每次调用都保持不变。
| 字段 | 类型 | 示例 | 说明 |
|---|---|---|---|
tweets.id | string | "1948167071684231344" | 推文 ID |
tweets.tweet_id | string | "1948167071684231344" | 推文 ID(与 id 值相同) |
tweets.url | string | "https://x.com/BillGates/status/194816707168…" | 推文的 URL |
tweets.text | string | "Learning how many kids were dying from diar…" | 推文文本 |
tweets.timestamp | string | "Wed Jul 23 23:43:19 +0000 2025" | 推文发布时间,按 X 的格式 |
tweets.lang | string | "en" | 推文的语言代码 |
tweets.links | array | 1 items | 推文中的链接 |
tweets.media | array | [] | 推文附带的媒体 |
tweets.likes | integer | 5219 | 点赞数 |
tweets.replies | integer | 0 | 回复数 |
tweets.retweets | integer | 1816 | 转推数 |
tweets.quotes | integer | 598 | 引用推文数 |
tweets.bookmarks | integer | 334 | 书签数 |
tweets.views | integer | 2828080 | 浏览量 |
tweets.isPinned | boolean | false | 推文被置顶时为 true |
tweets.isQuote | boolean | false | 推文引用了另一条推文时为 true |
tweets.isRetweet | boolean | false | 推文是转推时为 true |
tweets.isReply | boolean | false | 推文是回复时为 true |
tweets.username | string | "@BillGates" | 带 @ 的用户名 |
tweets.fullname | string | "Bill Gates" | 作者的显示名称 |
tweets.verified | boolean | true | 作者是否已认证 |
tweets.tweetUserId | string | "50393960" | 作者的数字用户 ID |
tweets.user | object | {…} | 作者对象:avatar, username, userFullName, description, location, website, joinDate, verified, totalLikes, totalTweets, totalFollowing, totalFollowers, url, userId, totalMediaCount |
tweets.input_username | string | "BillGates" | 你发送的 usernames 值 |
tweets.scraped_at | string | "2026-10-03T03:35:53.722Z" | Scrapier 读取该页面的时间(ISO 8601,UTC) |
success | boolean | true | 每个 200 响应都为 true |
data.usernames | string | "BillGates" | 你输入的主页,原样回传 |
data.user_id | string | "50393960" | 解析得到的数字用户 ID |
data.screen_name | string | "BillGates" | 解析得到的用户名 |
data.page | integer | 1 | 本次响应的页码 |
data.page_size | integer | 20 | 每页推文数(目录示例中为 20) |
data.next_cursor | string | "eyJoIjoiLi4uIiwiaW4iOiJCaWxsR2F0ZXMiLCJsdCI…" | 作为 cursor 传回即可获取下一页;最后一页为 null |
请求参数参考
| 名称 | 类型 | 必填 | 默认值 | 说明 | 示例 |
|---|---|---|---|---|---|
usernames | string | 是 | — | X 用户名、数字用户 ID 或主页 URL。每次调用一个。第一次调用时必填,设置 cursor 时被忽略。别名:username、user、users、profile | elonmusk |
cursor | string | 否 | — | 传回上一个响应的 data.next_cursor 以获取下一页;null 表示最后一页 | — |
with_replies | boolean | 否 | true | true(默认)包含回复,false 排除回复。锁定在游标中 | true |
include_user_info | boolean | 否 | true | true(默认)添加每条推文的 user 对象。锁定在游标中 | true |
use_proxy | boolean | 否 | true | 通过 Scrapier 的轮换住宅代理池路由,因为 X 会按 IP 限制匿名调用的速率。保持默认即可 | true |
raw_data | boolean | 否 | false | 在 raw_responses 下返回 X 的原始上游数据,而不是格式化后的条目 | true |
每次调用一个主页:要扫描十个主页,就调用十次,并用 cursor 为每个翻页。布尔值是字符串 true 或 false。
Twitter 主页抓取 API 的分页
Twitter 主页抓取 API 分页返回结果。把 data.next_cursor 作为 cursor 传回即可获取下一页;当它为 null 时就没有更多了。每次成功的调用,无论第一页还是之后的页,都收取 10 积分。
无需 X (Twitter) 登录即可访问
Twitter 主页抓取 API 不需要 X (Twitter) 账号、登录或会话 cookie。你在 X-API-Key 请求头中发送 Scrapier 密钥,即可获得 JSON。它只读取公开数据:任何访客不登录就能看到的内容。
速度、成本和并发
| 指标 | 数值 | 来源 |
|---|---|---|
| 成功调用的成本 | 10 积分 | Scrapier 目录 |
| 失败调用的成本 | 0 积分 | 任何非 2xx 响应 |
| 每个密钥的并行请求数 | 3 | 超过后:429 和 Retry-After |
| 建议的客户端超时 | 210 秒 | Scrapier 文档,适用于每个端点 |
Twitter 主页抓取 API 的延迟
Scrapier 没有公布该端点的延迟或成功率数据。每次调用都在调用时读取 X (Twitter),所以响应时间取决于 X (Twitter) 和结果的大小。请按文档对所有端点的建议,把客户端超时设为 210 秒。
端点的限制
Twitter 主页抓取 API 的如实限制。
- 仅限公开数据。
- 失败的调用,例如推文页面缺失或不可用,会返回非 2xx 状态,收取 0 积分。
- 数据在调用时实时读取。
- 每个密钥 3 个并行请求;超过后 API 会返回
429和Retry-After。 - 较大的结果分页返回;把
data.next_cursor作为cursor传回。 - X (Twitter) 不显示的字段不会返回。
Scrapier 与官方 X (Twitter) API 对比
当你需要代表用户发帖或操作,或需要只有账号所有者才能看到的数据时,官方 X API 是更好的选择。
| 功能 | Scrapier 推文页抓取器 | 官方 X (Twitter) API |
|---|---|---|
| 所需权限 | 一个 Scrapier API 密钥 | 开发者账号、应用凭据,以及带访问限制的套餐 |
| 哪些推文 | 任何公开主页的推文,可开关回复 | 取决于端点和你套餐的访问级别 |
| 返回的字段 | 每条推文 25 个字段,含文本、互动和作者 | 取决于你请求的字段和扩展 |
| 速率限制 | 每个密钥最多 3 个并行请求;超出返回 429 和 Retry-After | 按端点和套餐设定的限制 |
| 设置 | 注册,复制密钥,发送请求 | 申请访问权限、创建应用、管理令牌 |
| 成本 | 每页 10 积分,每 1,000 页低至 $1.20 | 由你选择的套餐决定 |
错误与重试处理
| 状态 | 含义 | 是否计费 | 解决办法 |
|---|---|---|---|
| 200 | 在 data.tweets 中返回的推文页数据 | 是 | — |
| 400 | 输入有误或缺失 | 否 | 检查 usernames:每次调用一个值 |
| 401 | 缺少 X-API-Key 请求头 | 否 | 添加该请求头 |
| 402 | 积分用完(响应体含 balance) | 否 | 充值;重试无济于事 |
| 403 | API 密钥无效或已撤销 | 否 | 在控制台中检查密钥 |
| 429 | 被限流 | 否 | 等待 Retry-After 秒后重试 |
| 5xx | 上游故障或超时 | 否 | 退避重试;把客户端超时设为 210 s |
来自错误参考。任何非 2xx 响应消耗 0 积分。
Twitter 主页抓取 API 的重试逻辑
import time, requests def get_profile_tweets(value, key, tries=5): for attempt in range(tries): r = requests.get( "https://proxy.scrapier.io/api/v2/twitter-tweets/", headers={"X-API-Key": key}, params={"usernames": value}, timeout=210, ) if r.status_code == 429: time.sleep(int(r.headers.get("Retry-After", 2 ** attempt))) elif r.status_code >= 500: time.sleep(2 ** attempt) else: r.raise_for_status() return r.json() raise RuntimeError("gave up after retries")
价格与速率限制
| 套餐 | 月费 | 包含的推文页数 | 每 1,000 次成本 |
|---|---|---|---|
| Hobby | $29 | 15,000 | $1.93 |
| Pro | $79 | 50,000 | $1.58 |
| Scale | $299 | 250,000 | $1.20 |
Twitter 主页抓取 API 的计费规则
Twitter 主页抓取 API 每成功获取一页推文收取 10 积分。失败的调用(即任何非 2xx 状态)收取 0 积分。每个套餐每个密钥允许 3 个并行请求。免费额度让你付费前先试用。
批量查询与 CSV
usernames 每次调用接受一个值。处理列表时循环调用,每个密钥最多并行 3 个请求,再把每一行展平成一行 CSV。
import csv, requests from concurrent.futures import ThreadPoolExecutor values = ["elonmusk"] # your list def fetch(value): r = requests.get( "https://proxy.scrapier.io/api/v2/twitter-tweets/", headers={"X-API-Key": "YOUR_API_KEY"}, params={"usernames": value}, timeout=210, ) r.raise_for_status() out = r.json()["data"]["tweets"] return out if isinstance(out, list) else [out] # 3 parallel requests per key is the limit with ThreadPoolExecutor(max_workers=3) as pool: rows = [row for part in pool.map(fetch, values) for row in part] cols = ["username", "fullname", "timestamp", "text", "likes", "retweets"] with open("out.csv", "w", newline="") as f: w = csv.DictWriter(f, fieldnames=cols, extrasaction="ignore") w.writeheader() w.writerows(rows)
从 Twitter 主页抓取 API 导出 CSV
username,fullname,timestamp,text,likes,retweets @BillGates,Bill Gates,Wed Jul 23 23:43:19 +0000 2025,Learning how many kids were dying from diarrhea back in 1997 ... https://t.co/NYitqiRY38,5219,1816
通过 MCP 供 AI 智能体访问
Twitter 主页抓取 API 的 MCP
AI 智能体可以通过 Scrapier MCP 服务器 https://mcp.scrapier.io/mcp 使用同一个端点。一条命令即可在 Claude Code 中注册:claude mcp add -t http scrapier https://mcp.scrapier.io/mcp --header "X-API-Key: YOUR_API_KEY"。智能体用 search_endpoints 找到该端点,再用 run_scrape 和端点 ID twitter-tweets 调用它。它使用相同的 usernames 输入,每页推文同样是 10 积分。各客户端的设置见 Twitter 主页抓取 MCP 页面。
按角色划分的集成路径
n8n 和 Make
- 添加一个 HTTP Request 步骤
- 带上你的
X-API-Key请求头,对该端点发送 GET - 把
data.tweets字段映射到你的列
循环并翻页
CSV 和展平的 JSON
把每一行展平为一行 CSV,每个字段一列,可直接用于 Sheets、Excel 或 BI 工具。
法律与合规说明
抓取公开可访问的 X (Twitter) 数据在许多国家通常是合法的,但具体取决于你收集什么、你身在何处以及如何使用。在美国,hiQ Labs v. LinkedIn 是谈到公开数据抓取时最常被引用的案例。Scrapier 只返回任何访客无需登录就能看到的公开推文和主页详情。根据 GDPR 及类似法律,收集公开数据可以是允许的,但你存储和使用个人数据(如姓名、用户名或帖子)的方式仍须符合当地的数据保护规定,并且适用 X 的条款。如果你的用例涉及大量存储个人数据,请咨询法律顾问。这不是法律意见;请参阅我们的使用条款。
常见问题
有获取 X (Twitter) 推文页面的 API 吗?
有。Scrapier's 的 Twitter 主页抓取 API 只需一次 GET 请求和一个 X-API-Key 请求头,就能以 JSON 返回任意公开的推文页面。无需 X (Twitter) 应用或登录。
如何从 Python 调用?
对 <code>https://proxy.scrapier.io/api/v2/twitter-tweets/</code> 使用 <code>requests.get</code>,带上 <code>params={'usernames': 'elonmusk'}</code> 和 <code>headers={"X-API-Key": KEY}</code>。读取 <code>r.json()["data"]["tweets"]</code>。
调用失败时会怎样?
任何非 200 响应都免费。对 429 和 5xx 请退避重试;API 已经会重试上游故障。
多少钱?
每次成功调用 10 积分,Pro 套餐下每 1,000 页推文约 $1.58。失败的调用免费。
有 OpenAPI 规范吗?
有。获取它无需密钥,并且涵盖每个 Scrapier 端点。