Python – Tweepy 中的 API.followers()
Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。
API.followers()
Tweepy 模块中API
类的followers()
方法用于获取指定用户的关注者在其中添加的顺序。
Syntax : API.followers(id / user_id / screen_name)
Parameters : Only use one of the 3 options:
id : specifies the ID or the screen name of the user
user_id : specifies the ID of the user, useful to differentiate accounts when a valid user ID is also a valid screen name
screen_name : specifies the screen name of the user, useful to differentiate accounts when a valid screen name is also a user ID
Returns : a list of objects of the class User
示例 1: followers() 方法返回 20 个最近的关注者。
# import the module
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the screen_name of the targeted user
screen_name = "geeksforgeeks"
# printing the latest 20 followers of the user
for follower in api.followers(screen_name):
print(follower.screen_name)
输出 :
ManojChevula
davidasem_
rajneesosho
sl_jens
Sandeep06081
abdulwhabalras2
chryptografio
_Mohit_Rajput_
elamineyamani
abhiSharma0311
PawanYa87321156
yagneshmb
yogeekabhi
DarioGallardo2
imharsan
MOHAMMA18757894
ArunKum79855492
ans_human_ap
1nonlyabhi
示例 2:使用Cursor()
方法可以访问超过 20 个关注者。
# the screen_name of the targeted user
screen_name = "geeksforgeeks"
# getting only 30 followers
for follower in tweepy.Cursor(api.followers, screen_name).items(30):
print(follower.screen_name)
输出 :
mr_manav852
MohsinI09002010
educatorly
JimXu_artist
JaiHin303
Indrajeet307
sainisajal147
aryangarg22
Ashutos11691851
ManojChevula
davidasem_
osho957
sl_jens
Sandeep06081
abdulwhabalras2
chryptografio
_Mohit_Rajput_
elamineyamani
abhiSharma0311
PawanYa87321156
yagneshmb
yogeekabhi
DarioGallardo2
imharsan
MOHAMMA18757894
ArunKum79855492
ans_human_ap
1nonlyabhi
SimplicitySol2
prateekmaj21
示例 3:计算关注者的数量。
# the screen_name of the targeted user
screen_name = "geeksforgeeks"
# getting all the followers
c = tweepy.Cursor(api.followers, screen_name)
# counting the number of followers
count = 0
for follower in c.items():
count += 1
print(screen_name + " has " + str(count) + " followers.")
输出 :
geeksforgeeks has 17338 followers.
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。