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


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

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

API.show_friendship()

的show_friendship()方法APITweepy模塊中的class用於獲取兩個用戶之間的詳細關係。

用法:API.show_friendship(source_id / source_screen_name, target_id / target_screen_name)

參數:

  • source_id:指定用戶的ID 1。
  • source_screen_name:指定用戶的屏幕名稱1。
  • target_id:指定用戶的ID 2。
  • target_screen_name:指定用戶的屏幕名稱2。

返回值:友誼類的對象



範例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 1 
source_screen_name = "Twitter"
  
# screen name of the account 2 
target_screen_name = "TwitterIndia"
  
# getting the friendship details 
friendship = api.show_friendship(source_screen_name = source_screen_name, target_screen_name = target_screen_name) 
  
print("Is " + friendship[0].screen_name + " followed by " + friendship[1].screen_name, end = "?:") 
if friendship[0].followed_by == False:
    print("No") 
else:
    print("Yes") 
  
print("Is " + friendship[0].screen_name + " following " + friendship[1].screen_name, end = "?:") 
if friendship[0].following == False:
    print("No") 
else:
    print("Yes")

輸出:

Is Twitter followed by TwitterIndia?:Yes
Is Twitter following TwitterIndia?:No

範例2:通過使用用戶ID分析友誼。

# user ID of the account 1 
source_id = 14230524
  
# user ID of the account 2 
target_id = 34507480
  
# getting the friendship details 
friendship = api.show_friendship(source_id = source_id, target_id = target_id) 
  
print("The sceen name of user 1 is:" + friendship[0].screen_name) 
print("The sceen name of user 2 is:" + friendship[1].screen_name) 
  
print("Is " + friendship[0].screen_name + " followed by " + friendship[1].screen_name, end = "?:") 
if friendship[0].followed_by == False:
    print("No") 
else:
    print("Yes") 
  
print("Is " + friendship[0].screen_name + " following " + friendship[1].screen_name, end = "?:") 
if friendship[0].following == False:
    print("No") 
else:
    print("Yes")

輸出:

The sceen name of user 1 is:ladygaga
The sceen name of user 2 is:ArianaGrande
Is ladygaga followed by ArianaGrande?:Yes
Is ladygaga following ArianaGrande?:No



相關用法


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