Python – Tweepy 中的 API.show_friendship()
Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。要做的第一件事是从 Twitter 开发人员那里轻松地为每个用户获取消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。
API.show_friendship()
Tweepy模块中API类的show_friendship()方法用于获取两个用户之间的详细关系。
Syntax : API.show_friendship(source_id / source_screen_name, target_id / target_screen_name)
Parameters :
- source_id : specifies the ID of the user 1.
- source_screen_name : specifies the screen name of the user 1.
- target_id : specifies the ID of the user 2.
- target_screen_name : specifies the screen name of the user 2.
Returns : an object of the class Friendship
示例 1:使用屏幕名称分析友谊。
Python3
# 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")
Python3
# 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 screen name of user 1 is : " + friendship[0].screen_name)
print("The screen 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")
输出 :
Is Twitter followed by TwitterIndia? : Yes
Is Twitter following TwitterIndia? : No
示例 2:使用用户 ID 分析友谊。
Python3
# 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 screen name of user 1 is : " + friendship[0].screen_name)
print("The screen 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 screen name of user 1 is : ladygaga
The screen name of user 2 is : ArianaGrande
Is ladygaga followed by ArianaGrande? : Yes
Is ladygaga following ArianaGrande? : No