📜  Python – Tweepy 中的 API.user_timeline()

📅  最后修改于: 2022-05-13 01:55:08.597000             🧑  作者: Mango

Python – Tweepy 中的 API.user_timeline()

Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。

API.user_timeline()

Tweepy 模块中API类的user_timeline()方法用于获取验证用户或指定用户发布的 20 个最新状态。

示例 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