如何使用 Tweepy 和 Python 取消关注 5 个用户
How to unfollow 5 users using Tweepy & Python
我想要 运行 一个使用 Tweepy 和 Python 一次只取消关注 5 个用户的脚本。以下脚本有效,但在我取消关注 5 个用户后它会继续运行。我如何让它在 5 点停止?感谢您的帮助!
import tweepy
import time
def get_twitter_api():
# personal details
consumer_key = "xxxx"
consumer_secret = "xxxx"
access_token = "xxxx"
access_token_secret = "xxxx"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
def process():
interval = 61
api = get_twitter_api()
followers = api.followers_ids(api.me().id)
print("Followers", len(followers))
friends = api.friends_ids(api.me().id)
print("You follow:", len(friends))
for friend in friends[::-1]:
if friend not in followers:
api.destroy_friendship(friend)
time.sleep(interval)
amount = 5
if __name__ == "__main__":
process()
您可以在 for
循环之前将变量设置为 0
。
然后,您可以在 if
语句中将变量递增 1
。
然后你可以在之后添加另一个if
语句,检查变量是否等于5
,如果是,则break
或return
。
例如:
amount = 0
for friend in friends[::-1]:
if friend not in followers:
api.destroy_friendship(friend)
time.sleep(interval)
amount += 1
if amount == 5:
break
你可能想 sleep
在递增和检查之前,如果你不希望它在最后一个之后等待。
我想要 运行 一个使用 Tweepy 和 Python 一次只取消关注 5 个用户的脚本。以下脚本有效,但在我取消关注 5 个用户后它会继续运行。我如何让它在 5 点停止?感谢您的帮助!
import tweepy
import time
def get_twitter_api():
# personal details
consumer_key = "xxxx"
consumer_secret = "xxxx"
access_token = "xxxx"
access_token_secret = "xxxx"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
def process():
interval = 61
api = get_twitter_api()
followers = api.followers_ids(api.me().id)
print("Followers", len(followers))
friends = api.friends_ids(api.me().id)
print("You follow:", len(friends))
for friend in friends[::-1]:
if friend not in followers:
api.destroy_friendship(friend)
time.sleep(interval)
amount = 5
if __name__ == "__main__":
process()
您可以在 for
循环之前将变量设置为 0
。
然后,您可以在 if
语句中将变量递增 1
。
然后你可以在之后添加另一个if
语句,检查变量是否等于5
,如果是,则break
或return
。
例如:
amount = 0
for friend in friends[::-1]:
if friend not in followers:
api.destroy_friendship(friend)
time.sleep(interval)
amount += 1
if amount == 5:
break
你可能想 sleep
在递增和检查之前,如果你不希望它在最后一个之后等待。