Python – Tweepy 中的 API.user_timeline()
Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。
API.user_timeline()
Tweepy 模块中API
类的user_timeline()
方法用于获取验证用户或指定用户发布的 20 个最新状态。
Syntax : API.user_timeline(parameters)
Parameters :
- 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.
- since_ids : Fetch only the statuses newer than the specified ID.
- max_ids : Fetch only the statuses older than or equal to the specified ID.
- count : The number of statuses to be fetched, the default value is 20.
Returns : a list of objects of the class Status
示例 1:
# 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)
# screen name of the account to be fetched
screen_name = "geeksforgeeks"
# fetching the statuses
statuses = api.user_timeline(screen_name)
print(str(len(statuses)) + " number of statuses have been fetched.")
输出 :
20 number of statuses have been fetched.
示例 2:使用带有 count 参数的user_timeline()
方法仅获取指定数量的状态。
# screen name of the account to be fetched
screen_name = "geeksforgeeks"
# number of statuses to be fetched
count = 3
# fetching the statuses
statuses = api.user_timeline(screen_name, count = count)
# printing the statuses
for status in statuses:
print(status.text, end = "\n\n")
输出 :
Hola Geeks!
.
Tell us what did you learned during this lockdown?
.
#MondayMotivation #mondaythoughts #codinglife… https://t.co/1mVLOKdaIL
@ZomatoIN Avoid Errors, not Client calls
#sundayvibes #programmingmemes
Avoid errors, not client calls
.
Geeks, Keep this going...
.
#sundayvibes #programming #programmingmemes #coding https://t.co/JkA5iStofZ
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。