使用 Tweepy 从用户的 Twitter 关注者获取用户 ID 列表

Get list of user id's from a users Twitter followers using Tweepy

我已经编写了以下代码来获取用户名拥有的所有 Twitter 关注者的列表,但我只想获取 user_id,因为这允许我在速率限制内做更多事情对于 API。如何更改此代码以仅打印 user_ids?

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth)

screen_name = "twitterusername"
  
friends = api.friends_ids(screen_name) 
  
print(screen_name + " is following :") 
for friend in friends: 
    print(api.get_user(friend).screen_name)

我拥有所有正确的授权密钥和令牌。

用户对象有一个 id 字段

print(screen_name + " is following :") 
for friend in friends: 
  print(api.get_user(friend).id)

如果您只对用户的 ID 感兴趣,请注意 api.friends_ids returns 已经是一个 ID 列表

friends = api.friends_ids('screen_name')
logging.info(friends)
# output: [324343434, 134343234567,...]