Python达到了Tweepy的速率限制

时间:2017-03-02 02:26:48

标签: python tweepy

我目前正在制作一个Twitter刮刀,我希望得到所有带有多个主题标签的推文。问题是我每次尝试传递第一个#标签时都会收到429个错误。我试过睡觉这个功能,但是每当第二个标签出现时,它都无法正常工作。

import tweepy
import time
import json
from collections import defaultdict as dd

f = open("tokens.txt", 'r')
consumer_key = f.readline().strip()
consumer_secret = f.readline().strip()
app_key = f.readline().strip()
app_secret = f.readline().strip()

auth =tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(app_key,app_secret)

api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)



usercount = dd(int)
userfollowers = dd(int)
mostretweets = dd(int)
mostfav = dd(int)

hashtag = ['#csforall','#equality']
for i in hashtag :
    for status in tweepy.Cursor(api.search, q=i,since="2017-02-25",until="2017-02-28",lang="en").items():
        parsed = status._json
        usercount[parsed['user']['name'].encode("utf-8")]+=1
        userfollowers[parsed['user']['name'].encode("utf-8")]= parsed['user']['followers_count']
        mostretweets[parsed['text'].encode('utf-8')] = parsed['retweet_count']         
        mostfav[parsed['text'].encode('utf-8')] = parsed['favorite_count'] 

        time.sleep(2) 



    sortcount = sorted(usercount.items(), key=lambda x: x[1], reverse =True)
    top = sortcount[:1]
    frequser=[]
    for i in sortcount:
        if i[1] == top:
            frequser.append(i)
        else:
            break       
    print ("Top most frequent user: \n " + str(i[0])) +"\n"

    followcount = sorted(userfollowers.items(), key=lambda x: x[1], reverse =True)
    fol = followcount[:1]
    freqfollow = []
    for j in followcount:
        if j[1] == fol:
            freqfollow.append(i)
        else:
            break     
    print ("User with most followers: \n " + str(j[1])) 


    retweetcount = sorted(mostretweets.items(), key=lambda x: x[1], reverse = True)
    ret = retweetcount[:1]
    freqretweet =[]
    for i in retweetcount:
        if i[1] == ret:
            freqretweet == ret
        else:
            break
    print str(i[0])+"\n"

    favcount = sorted(mostfav.items(), key=lambda x: x[1], reverse = True)
    ret = favcount[:1]
    freqfav =[]
    for i in favcount:
        if i[1] == ret:
            freqfav == ret
        else:
            break
    print str(i[0])+"\n"

1 个答案:

答案 0 :(得分:0)

放这个:

for i in hashtag:
    time.sleep(2)
    for status in tweepy.Cursor(api.search, q=i,since="2017-02-25",until="2017-02-28",lang="en").items():  

工作?