You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tweepy调用get_followed_lists无返回结果求可行示例

使用Tweepy结合Twitter API v2获取用户关注列表的正确示例

你之前的代码返回空结果,核心问题是用错了方法:get_followed_lists是用来获取用户订阅的Twitter列表(比如主题类聚合列表),而不是用户关注的其他账号。要获取用户关注的用户列表,应该调用get_users_following方法。

以下是可行的实现示例,包含分页处理(API单次最多返回100条结果,超过需要分页拉取):

import tweepy

# 替换为你的Bearer Token
BEARER_TOKEN = "<你的Bearer Token>"

# 初始化Tweepy客户端
client = tweepy.Client(bearer_token=BEARER_TOKEN, wait_on_rate_limit=True)

# 目标用户名
target_username = "IOHK_Charles"

# 第一步:通过用户名获取用户ID
user = client.get_user(username=target_username)
user_id = user.data.id

# 第二步:获取用户关注的所有用户,处理分页
following_list = []
pagination_token = None

while True:
    # 调用get_users_following,每次最多拉100条,支持分页
    response = client.get_users_following(
        id=user_id,
        max_results=100,
        pagination_token=pagination_token,
        user_fields=["username", "name", "public_metrics"]  # 指定要返回的用户字段
    )
    
    if not response.data:
        break
    
    following_list.extend(response.data)
    pagination_token = response.meta.get("next_token")
    
    # 没有下一页令牌时停止循环
    if not pagination_token:
        break

# 输出结果示例
print(f"用户 {target_username} 共关注 {len(following_list)} 个账号:")
for user in following_list[:5]:  # 只打印前5个示例
    print(f"用户名:@{user.username},昵称:{user.name},粉丝数:{user.public_metrics['followers_count']}")

注意事项:

  • 确保你的Bearer Token在Twitter开发者平台拥有read:users权限,否则会返回权限错误。
  • 如果目标用户关注的账号超过100个,必须通过分页令牌(next_token)循环拉取所有结果,否则只能拿到前100条。
  • wait_on_rate_limit=True会让Tweepy自动处理API速率限制,达到限制时自动等待,无需手动处理。

内容的提问来源于stack exchange,提问作者Reddspark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 18:20:34