1

我编写了以下代码来获取用户名拥有的所有 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)

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

4

1 回答 1

1

用户对象有一个id字段

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

如果您只对用户的 ID 感兴趣,请注意已api.friends_ids返回 ID 列表

friends = api.friends_ids('screen_name')
logging.info(friends)
# output: [324343434, 134343234567,...]
于 2020-11-16T08:00:41.993 回答