使用 Tweepy 检查用户是否关注我
Check if a user is following me using Tweepy
我正在使用 Tweepy,我想创建一个脚本来取消关注那些不关注我的人。我轻松地创建了相反的东西:
for user in tweepy.Cursor(api.followers).items():
if not user.following:
user.follow()
但是在 api.friends
中似乎没有 属性 判断用户是否关注我。
您可以使用 API.exists_friendship(user_a, user_b)
。如果 user_a
跟随 user_b
.
,则 returns 为真
参考:http://docs.tweepy.org/en/v3.5.0/api.html#API.exists_friendship
一年后,我发现自己需要(再次)解决这个问题。这次我在 Python 方面更有经验,所以我可以找到一种行之有效的方法。
每100个用户只需要调用一次API,这是_lookup_friendships
方法一次可以查询到的最大用户数。好吧,那,+ 取消关注,当然。
for page in tweepy.Cursor(api.friends, count=100).pages():
user_ids = [user.id for user in page]
for relationship in api._lookup_friendships(user_ids):
if not relationship.is_followed_by:
logger.info('Unfollowing @%s (%d)',
relationship.screen_name, relationship.id)
try:
api.destroy_friendship(relationship.id)
except tweepy.error.TweepError:
logger.exception('Error unfollowing.')
我认为您可以使用以下代码来获取一个用户关注的所有社交媒体用户:
# Here 1392368760 is just an example user
friends = api.friends_ids(1392368760)
print("This user follow", len(friends), "users")
friends
是一个列表,包含用户1392368760
关注的所有人的用户id。
对于您的情况,您可以将 1392368760
替换为您感兴趣的用户,然后检查您是否在 friends
列表中。此外,您的个人用户名可以通过以下代码访问:
api.me().id
以下是使用 Tweepy 4.6.0 完成此操作的方法。
my_screen_name = api.get_user(screen_name='YOUR_SCREEN_NAME')
for follower in my_screen_name.friends():
Status = api.get_friendship(source_id = my_screen_name.id , source_screen_name = my_screen_name.screen_name, target_id = follower.id, target_screen_name = follower.screen_name)
if Status [0].followed_by:
print('{} he is following You'.format(follower.screen_name))
else:
print('{} he is not following You'.format(follower.screen_name))
api.destroy_friendship(screen_name = follower.screen_name)
我正在使用 Tweepy,我想创建一个脚本来取消关注那些不关注我的人。我轻松地创建了相反的东西:
for user in tweepy.Cursor(api.followers).items():
if not user.following:
user.follow()
但是在 api.friends
中似乎没有 属性 判断用户是否关注我。
您可以使用 API.exists_friendship(user_a, user_b)
。如果 user_a
跟随 user_b
.
参考:http://docs.tweepy.org/en/v3.5.0/api.html#API.exists_friendship
一年后,我发现自己需要(再次)解决这个问题。这次我在 Python 方面更有经验,所以我可以找到一种行之有效的方法。
每100个用户只需要调用一次API,这是_lookup_friendships
方法一次可以查询到的最大用户数。好吧,那,+ 取消关注,当然。
for page in tweepy.Cursor(api.friends, count=100).pages():
user_ids = [user.id for user in page]
for relationship in api._lookup_friendships(user_ids):
if not relationship.is_followed_by:
logger.info('Unfollowing @%s (%d)',
relationship.screen_name, relationship.id)
try:
api.destroy_friendship(relationship.id)
except tweepy.error.TweepError:
logger.exception('Error unfollowing.')
我认为您可以使用以下代码来获取一个用户关注的所有社交媒体用户:
# Here 1392368760 is just an example user
friends = api.friends_ids(1392368760)
print("This user follow", len(friends), "users")
friends
是一个列表,包含用户1392368760
关注的所有人的用户id。
对于您的情况,您可以将 1392368760
替换为您感兴趣的用户,然后检查您是否在 friends
列表中。此外,您的个人用户名可以通过以下代码访问:
api.me().id
以下是使用 Tweepy 4.6.0 完成此操作的方法。
my_screen_name = api.get_user(screen_name='YOUR_SCREEN_NAME')
for follower in my_screen_name.friends():
Status = api.get_friendship(source_id = my_screen_name.id , source_screen_name = my_screen_name.screen_name, target_id = follower.id, target_screen_name = follower.screen_name)
if Status [0].followed_by:
print('{} he is following You'.format(follower.screen_name))
else:
print('{} he is not following You'.format(follower.screen_name))
api.destroy_friendship(screen_name = follower.screen_name)