如何用 TwexAPI 批量获取 X 用户资料
当你已经有一批 X handle,下一步通常是资料补全:把 elonmusk、sundarpichai,或者一列 profile URL,变成 CRM、研究数据库或仪表盘可以使用的结构化 profile 记录。
TwexAPI 的 Get Multiple Users 端点就是做这个的。把用户名或 X profile URL 数组发送到 POST /twitter/users,再把返回的用户对象规范化,并清楚记录哪些账号没有找到。
什么时候用这个端点
当输入是一批用户名或 profile URL,并且你需要批量获取用户资料元数据时,使用 POST /twitter/users。
典型场景包括:
- 给影响者表格补充名称、简介、位置、认证字段和响应中存在的粉丝数。
- 在营销活动前刷新 KOL 数据库。
- 检查社区名单里的 handle 是否还能解析到有效 profile。
- 为从推文、回复、List 或搜索结果中采集的数据补充作者信息。
如果你的输入是数字 user ID,而不是用户名,请改用 POST /twitter/users/by_ids。
端点与请求体
发送 POST 请求到:
https://api.twexapi.io/twitter/users请求体是字符串数组。每个字符串可以是用户名,也可以是 profile URL。
curl --request POST \
--url https://api.twexapi.io/twitter/users \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '[
"elonmusk",
"sundarpichai",
"https://x.com/tim_cook"
]'API 返回对象包含 code、msg 和 data。data 数组里是用户资料对象。如果某个用户名找不到,对应位置可能是 null,所以代码里要显式处理缺失行。
1{
2 "code": 200,
3 "msg": "success",
4 "data": [
5 {
6 "userId": "44196397",
7 "username": "elonmusk",
8 "name": "Elon Musk",
9 "description": "<profile bio>",
10 "followersCount": 1000000,
11 "isBlueVerified": true,
12 "verified": true
13 },
14 null
15 ]
16}不要把响应字段直接当成固定数据库 schema。建议先保存原始 profile 对象,再映射产品真正需要的字段。
Python 批量补全脚本
下面脚本会读取一组 handle,分批调用 POST /twitter/users,写出规范化 JSONL,并记录找不到的输入。
1import json
2import time
3from datetime import datetime, timezone
4from pathlib import Path
5
6import requests
7
8TOKEN = "<your_bearer_token>"
9URL = "https://api.twexapi.io/twitter/users"
10INPUTS = [
11 "elonmusk",
12 "sundarpichai",
13 "https://x.com/tim_cook",
14]
15BATCH_SIZE = 20
16OUT = Path("x-user-profiles.jsonl")
17MISSING = Path("x-user-profiles-missing.json")
18
19headers = {
20 "Authorization": f"Bearer {TOKEN}",
21 "Content-Type": "application/json",
22}
23
24def chunks(items, size):
25 for index in range(0, len(items), size):
26 yield items[index:index + size]
27
28missing = []
29
30with OUT.open("w", encoding="utf-8") as f:
31 for batch in chunks(INPUTS, BATCH_SIZE):
32 response = requests.post(URL, headers=headers, json=batch, timeout=30)
33 response.raise_for_status()
34 body = response.json()
35 profiles = body.get("data") or []
36
37 for original_input, profile in zip(batch, profiles):
38 if profile is None:
39 missing.append(original_input)
40 continue
41
42 row = {
43 "input": original_input,
44 "user_id": profile.get("userId") or profile.get("user_id"),
45 "username": profile.get("username") or profile.get("screen_name"),
46 "name": profile.get("name"),
47 "description": profile.get("description"),
48 "followers_count": profile.get("followersCount") or profile.get("followers_count"),
49 "verified": profile.get("verified"),
50 "is_blue_verified": profile.get("isBlueVerified") or profile.get("is_blue_verified"),
51 "fetched_at": datetime.now(timezone.utc).isoformat(),
52 "raw": profile,
53 }
54 f.write(json.dumps(row, ensure_ascii=False) + "\n")
55
56 time.sleep(1)
57
58MISSING.write_text(json.dumps({
59 "fetched_at": datetime.now(timezone.utc).isoformat(),
60 "missing": missing,
61}, ensure_ascii=False, indent=2), encoding="utf-8")
62
63print(f"Saved profiles to {OUT}; missing inputs: {len(missing)}")批大小要结合你的套餐和容错需求来选。小批次更容易重试,也更容易定位哪一批输入失败。
处理找不到或改名的账号
批量 profile 补全经常会遇到 handle 改名、账号停用、profile URL 带多余参数等情况。让整个 lookup 过程可观察:
- 把原始输入和 API 返回的规范化用户名一起保存。
- 将
null结果写入单独的 missing 文件,不要静默丢弃。 - 对 missing 账号定期复查,再决定是否永久标记为不可用。
- 去重前先统一大小写,并去掉 URL 前缀。
- 保存抓取时间,因为 profile 元数据会变化。
这样输出既能用于产品流程,也方便后续审计。
已有 User ID 时怎么做
如果你的源数据已经有数字 user ID,调用:
POST https://api.twexapi.io/twitter/users/by_ids请求体同样是字符串数组,但每一项是 user ID。
curl --request POST \
--url https://api.twexapi.io/twitter/users/by_ids \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '[
"44196397",
"1696160451770429440"
]'当你已经有 user ID 时,它更适合做稳定关联;当输入来自表格、profile URL、影响者名单或人工调研时,用用户名更方便。
放进数据管道时怎么设计
一个干净的 profile enrichment 管道通常分四步:
- 从搜索、List 成员、推文作者、回复或表格中收集候选 handle。
- 规范化 handle,并去重。
- 分批调用
POST /twitter/users,保存原始响应。 - 把应用需要的字段映射到稳定表结构中。
做分析时,不建议只用粉丝数排序。若结果会影响 outreach 或预算,请结合近期发帖、互动质量、主题匹配度和人工复核。
小结
按用户名补全资料时,用 POST /twitter/users,请求体传用户名或 profile URL 的 JSON 数组。保存原始 profile,处理 null 结果,并把原始输入和规范化后的 profile 放在一起。
这样能把一份杂乱的 X handle 列表变成可用的用户资料表,同时不会假装这些 profile 数据永远稳定。