我正在研究一个python twitter脚本。到目前为止它工作得很好,但是我需要添加一个while语句来将unfollow(api调用)的数量限制为小于100.我整天都在努力工作,我承认我是python的新手,我可能是遗漏了一些简单的东西,但我尝试了一些变化无济于事。
#! /usr/bin/python
# -*- coding: utf-8 -*-
import tweepy
from keys import keys
SCREEN_NAME = keys['screen_name']
CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
followers = api.followers_ids(SCREEN_NAME)
friends = api.friends_ids(SCREEN_NAME)
for f in friends:
if f not in followers:
print "Unfollow {0}?".format(api.get_user(f).screen_name)
if raw_input("Y/N?") == 'y' or 'Y':
api.destroy_friendship(f)
答案 0 :(得分:1)
添加取消关注计数器,并在进行取消关注调用时递增该计数器。然后检查计数器是否小于100,除了' y'答案。
#! /usr/bin/python
# -*- coding: utf-8 -*-
...code before for loop here...
unfollows = 0
for f in friends:
if f not in followers:
print "Unfollow {0}?".format(api.get_user(f).screen_name)
input = raw_input("Y/N?")
if (unfollows < 100) and (input == 'y' or input 'Y'):
api.destroy_friendship(f)
unfollows += 1