我如何用Tweepy计算Twitter用户的关注者

2024-04-16 18:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Tweepy的帮助下提取关注者并跟踪不同Twitter用户的数量,但我不断得到错误,[{'code': 89, 'message': 'Invalid or expired token.'}]

我所需要的只是数量(不是追随者/朋友的名单),因为有些人有数百万追随者。我本可以手动检查每个Twitter句柄,但这是不可行的,因为我有近20000个用户,我正在尝试检查

多谢各位

import tweepy
from requests_oauthlib import OAuth1

# Keys, tokens and secrets
consumer_key = " my api_key"
consumer_secret = "my api_secret_key"
key = "my access_token"
secret = " my access_token_secret"

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


targets = ['FoxNews', 'XHNews', 'Reuters', 'AP', 'MTV', 'CNN']

for target in targets:
    user = api.get_user(target)
    print(user.name, user.followers_count)

输出:TweepError: [{'code': 89, 'message': 'Invalid or expired token.'}]


Tags: key用户tokenauthapi数量secretaccess