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


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

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

API.lookup_friendships()

的lookup_friendships()方法APITweepy模塊中的類用於獲取經過身份驗證的用戶與用戶列表之間的詳細關係,一次最多100個。

用法:API.lookup_friendships(user_ids / screen_names)

參數:僅使用以下兩個選項之一:

  • user_ids:指定用戶ID的列表。
  • screen_names:指定用戶的屏幕名稱的列表。

返回值:類Relationship的對象



範例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 screen names 
screen_names = ["SrBachchan", 
                "akshaykumar", 
                "imVkohli", 
                "sachin_rt", 
                "SonuSood"] 
  
# getting the friendship details 
friendships = api.lookup_friendships(screen_names = screen_names) 
  
for friendship in friendships:
    print("Is the authenticated user following " + friendship.screen_name, end = "?:") 
    print(friendship.is_following)

輸出:

Is the authenticated user following SrBachchan?:False
Is the authenticated user following akshaykumar?:True
Is the authenticated user following imVkohli?:True
Is the authenticated user following sachin_rt?:False
Is the authenticated user following SonuSood?:False

範例2:通過使用用戶ID分析關係。

# list of user IDs 
user_ids = [813286, 
            27260086, 
            21447363, 
            79293791, 
            17919972] 
  
# getting the friendship details 
friendships = api.lookup_friendships(user_ids = user_ids) 
  
for friendship in friendships:
    print("Is the authenticated user following " + friendship.screen_name, end = "?:") 
    print(friendship.is_following)

輸出:

Is the authenticated user following BarackObama?:True
Is the authenticated user following justinbieber?:False
Is the authenticated user following katyperry?:False
Is the authenticated user following rihanna?:False
Is the authenticated user following taylorswift13?:False



相關用法


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