【发布时间】:2022-03-16 16:19:13
【问题描述】:
我正在使用 Tweepy,我想创建一个脚本来取消关注那些不关注我的人。我轻松地创造了相反的东西:
for user in tweepy.Cursor(api.followers).items():
if not user.following:
user.follow()
但在api.friends 中似乎没有一个属性来保存用户是否关注我。
【问题讨论】:
标签: tweepy
我正在使用 Tweepy,我想创建一个脚本来取消关注那些不关注我的人。我轻松地创造了相反的东西:
for user in tweepy.Cursor(api.followers).items():
if not user.following:
user.follow()
但在api.friends 中似乎没有一个属性来保存用户是否关注我。
【问题讨论】:
标签: tweepy
您可以使用API.exists_friendship(user_a, user_b)。如果user_a 跟随user_b,则返回true。
参考:http://docs.tweepy.org/en/v3.5.0/api.html#API.exists_friendship
【讨论】:
tweepy.Cursor(api.followers).items(200): 将其限制为 200
一年后,我发现自己需要(再次)解决这个问题。这次我在 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.')
【讨论】:
_lookup_friendships的?
我认为您可以使用以下代码来获取一位用户关注的所有社交媒体用户:
# 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)
【讨论】: