如何用 TwexAPI 搜索 X 社区内的帖子
X Communities 往往比主时间线更聚焦,讨论上下文也更清楚。如果你已经知道社区 ID,并想在这个社区里查找某个话题的帖子,可以使用 POST https://api.twexapi.io/twitter/community/search-tweets。
当全站搜索太吵时,这个端点更合适。你不是收集某个关键词在全网的所有公开提及,而是在一个具体社区里搜索,并复核来自该社区参与者的帖子。
这个端点做什么
POST /twitter/community/search-tweets 用于在单个 X Community 内搜索 tweets。
请求包含三个必填字段:
| 字段 | 类型 | 用途 |
|---|---|---|
community_id | string | 要搜索的 X Community ID。 |
target_count | integer | 要搜索的 tweet 数量。调试查询时建议先用较小值。 |
query | string | 在社区内匹配的关键词或搜索短语。 |
适合的工作流包括:
- 复核某个垂直社区如何讨论产品、话题或发布。
- 为社区报告或内部摘要收集帖子。
- 追踪自有社区里的高频问题。
- 对比同一话题在多个社区里的表达差异。
该端点返回的是社交帖子,不能替代版规判断、用户研究或人工复核。
基础请求
curl --request POST \
--url https://api.twexapi.io/twitter/community/search-tweets \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"community_id": "1234567890123456789",
"target_count": 50,
"query": "customer support"
}'成功响应包含 code、msg 和 data。data 中每一项都是 tweet 对象。常用字段包括 tweet_id、text、full_text、created_at、created_at_datetime、favorite_count、retweet_count、reply_count、quote_count、hashtags、cashtags、media 和用户信息。
1{
2 "code": 200,
3 "msg": "success",
4 "data": [
5 {
6 "tweet_id": "1803006263529541838",
7 "text": "Example community post about customer support",
8 "created_at_datetime": "2024-06-17T03:51:48.000Z",
9 "favorite_count": 18,
10 "retweet_count": 3,
11 "reply_count": 6,
12 "hashtags": ["support"]
13 }
14 ]
15}Python 客户端
先把客户端保持得足够小。由调用方决定如何排序、存储和复核返回的帖子。
1import os
2from typing import Any
3
4import requests
5
6API_URL = "https://api.twexapi.io/twitter/community/search-tweets"
7TOKEN = os.environ["TWEXAPI_BEARER_TOKEN"]
8
9def search_community_tweets(
10 community_id: str,
11 query: str,
12 *,
13 target_count: int = 50,
14) -> list[dict[str, Any]]:
15 payload = {
16 "community_id": community_id,
17 "target_count": target_count,
18 "query": query,
19 }
20
21 response = requests.post(
22 API_URL,
23 headers={
24 "Authorization": f"Bearer {TOKEN}",
25 "Content-Type": "application/json",
26 },
27 json=payload,
28 timeout=30,
29 )
30 response.raise_for_status()
31 return response.json().get("data", [])
32
33if __name__ == "__main__":
34 tweets = search_community_tweets(
35 "1234567890123456789",
36 "customer support",
37 target_count=25,
38 )
39 for tweet in tweets[:5]:
40 text = tweet.get("full_text") or tweet.get("text") or ""
41 print(tweet.get("tweet_id"), text[:140])整理成复核行
不要一开始就做复杂大屏。先把每条 tweet 变成稳定的行数据,让分析人员或社区运营可以快速复核。
1def to_review_row(tweet: dict[str, Any]) -> dict[str, Any]:
2 favorite_count = tweet.get("favorite_count") or 0
3 retweet_count = tweet.get("retweet_count") or 0
4 reply_count = tweet.get("reply_count") or 0
5 quote_count = tweet.get("quote_count") or 0
6 engagement = favorite_count + retweet_count + reply_count + quote_count
7
8 return {
9 "tweet_id": tweet.get("tweet_id"),
10 "created_at": tweet.get("created_at_datetime") or tweet.get("created_at"),
11 "engagement": engagement,
12 "hashtags": ", ".join(tweet.get("hashtags") or []),
13 "text": tweet.get("full_text") or tweet.get("text") or "",
14 "url": f"https://x.com/i/status/{tweet.get('tweet_id')}",
15 }
16
17tweets = search_community_tweets(
18 "1234567890123456789",
19 "pricing",
20 target_count=100,
21)
22rows = [to_review_row(tweet) for tweet in tweets]
23rows.sort(key=lambda row: row["engagement"], reverse=True)
24
25for row in rows[:10]:
26 print(row["engagement"], row["url"], row["text"][:100])建议同时保存原始 tweet payload 和整理后的行数据。原始 payload 可以让你之后重新使用字段,而不必再次搜索。
多社区对比
如果要在多个社区里对比同一个 query,最好显式循环。这样更容易记录失败,也能避免把不同受众的结果混在一起。
1communities = {
2 "1234567890123456789": "Product builders",
3 "2345678901234567890": "Support leaders",
4 "3456789012345678901": "AI operators",
5}
6
7query = "onboarding"
8summary = []
9
10for community_id, name in communities.items():
11 tweets = search_community_tweets(community_id, query, target_count=50)
12 rows = [to_review_row(tweet) for tweet in tweets]
13 total_engagement = sum(row["engagement"] for row in rows)
14 summary.append(
15 {
16 "community_id": community_id,
17 "name": name,
18 "tweet_count": len(rows),
19 "total_engagement": total_engagement,
20 }
21 )
22
23for item in sorted(summary, key=lambda row: row["total_engagement"], reverse=True):
24 print(item["name"], item["tweet_count"], item["total_engagement"])这些数字只适合做初筛,不适合直接下结论。小社区的帖子可能更少,但上下文更准确。
监控流程
如果要持续监控,可以定时运行查询,并按 tweet_id 去重:
- 用更窄的
query搜索社区。 - 保存每条返回结果的
tweet_id。 - 跳过已经处理过的帖子。
- 按互动和发布时间排序新结果。
- 只把优先级最高的帖子送进人工复核队列。
如果你需要某个社区的未过滤 feed,TwexAPI 也提供 GET /twitter/community/{community_id}/tweets/{tweet_type}/{target_count} 和分页社区帖子接口。当关键词比完整 feed 更重要时,再使用 search endpoint。
常见坑
- 这个端点需要
community_id,不要传社区 slug。 query要具体。单词级查询通常会混入很多不同意图。- 把
target_count当作采集上限,不要当作每条结果都一定有用的保证。 - 定时任务开始前,先把
tweet_id作为去重键。 - 报告引用前先人工复核。社区帖子可能包含玩笑、反讽、跑题回复和私有上下文。
小结
POST /twitter/community/search-tweets 适合在单个 X Community 内做聚焦研究。传入 community_id、query 和 target_count,再把响应整理成可复核的行数据。
这个框架更稳:采集帖子,保留上下文,再由人判断哪些内容真的重要。