Tweepy 是一个流行的 Python 库,用于访问 Twitter API,从而实现与 Twitter 平台的交互。以下是如何使用 Tweepy 库在 Twitter 上获取好友和追随者的步骤:
以下是使用 Tweepy 获取好友和追随者的基本示例代码:
import tweepy
# 设置你的 Twitter API 认证信息
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'
# 认证并创建 API 对象
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# 获取好友列表
try:
friends = api.friends()
print("Friends:")
for friend in friends:
print(friend.screen_name)
except tweepy.TweepError as e:
print(f"Error getting friends: {e}")
# 获取追随者列表
try:
followers = api.followers()
print("Followers:")
for follower in followers:
print(follower.screen_name)
except tweepy.TweepError as e:
print(f"Error getting followers: {e}")
RateLimitHandler
。通过以上步骤和代码示例,你应该能够在 Twitter 上成功获取好友和追随者的信息。记得遵守 Twitter 的服务条款和 API 使用规则。
领取专属 10元无门槛券
手把手带您无忧上云