當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Tweepy API.statuses_lookup()用法及代碼示例


Twitter是一種流行的社交網絡,用戶在其中共享稱為推文的消息。 Twitter允許我們使用Twitter API或Tweepy挖掘任何用戶的數據。數據將是從用戶提取的推文。首先要做的是從每個用戶都可以輕鬆獲得Twitter開發人員的使用者 key ,使用者 key ,訪問 key 和訪問 key 。這些 key 將幫助API進行身份驗證。

API.statuses_lookup()

的statuses_lookup()方法APITweepy模塊中的class用於獲取由狀態ID指定的狀態,最多100個。

用法:API.statuses_lookup(parameters)

參數:

  • id_:要獲取的Tweet ID列表,最多100個
  • trim_user:一個布爾值,指示是否應提供用戶ID,而不是完整的用戶對象,默認值為False。

返回值:狀態類的對象列表



範例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) 
  
# list of status IDs to be fetched  
id_ = [1266978261701210112, 1266735261012111360, 1266342841648898049] 
  
# fetching the statuses 
statuses = api.statuses_lookup(id_) 
  
# printing the statuses 
for status in statuses:
    print("The status " + str(status.id) + " is posted by " + status.user.screen_name) 
    print("This status says:\n\n" + status.text, end = "\n\n")

輸出:

The status 1266978261701210112 is posted by geeksforgeeks
This status says:

Avoid errors, not client calls
.
Geeks, Keep this going...
.
#sundayvibes #programming #programmingmemes #coding https://t.co/JkA5iStofZ

The status 1266735261012111360 is posted by geeksforgeeks
This status says:

With the access to our Job Portal, find the jobs that are best for you & experience happy placement journey....
.
L… https://t.co/mzMMFVzjMv

The status 1266342841648898049 is posted by geeksforgeeks
This status says:

My reaction to this Lockdown:

"Der Lagi Lekin... Maine Ab Hai Jeena Seekh Liya" 

What's your reaction to it?
.… https://t.co/nH9L0eewSr

範例2:使用statuses_lookup()trim_user參數的方法。

# list of status IDs to be fetched  
id_ = [1266978261701210112, 1266735261012111360, 1266342841648898049] 
  
# fetching the statuses 
statuses = api.statuses_lookup(id_, trim_user = True) 
  
# printing the statuses 
for status in statuses:
    print(status.user.id)

輸出:

57741058
57741058
57741058



相關用法


注:本文由純淨天空篩選整理自Yash_R大神的英文原創作品 Python – API.statuses_lookup() in Tweepy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。