3

我正在尝试获取前一天的所有推文。为了解决 Twitter 的速率限制,我实现了两组代码。

if counter == 4000:
    time.sleep(60*20) # wait for 20 min every time 4,000 tweets are extracted 
    counter == 0
    continue

我查看了输出文件,通常当我提取了大约 5500-6500 个推文实体时,我会收到速率限制消息。所以为了保守起见,我设置每次提取 4000 条推文(以及相关的提取字段)时,我将其暂停 20 分钟(以覆盖 Twitter 指定的 15 分钟间隔)。

我还发现其他人尝试使用以下代码解决相同的问题:

except tweepy.TweepError:
    time.sleep(60*20)
    continue

它应该在出现 TweepError 时暂停脚本,我测试了它,但它似乎不起作用,但我还是包含了它。

我得到的错误(提取 10,700 个推文实体后)如下:

Traceback (most recent call last):
File "C:\Users\User\Dropbox\Python exercises\_Scraping\Social media\TweepyModule\TweepyTut1.18.py", line 32, in <module>
since='2014-09-15', until='2014-09-16').items(999999999): # changeable here
File "C:\Program Files Extra\Python27\lib\site-packages\tweepy\cursor.py", line 181, in next
self.current_page = self.page_iterator.next()
File "C:\Program Files Extra\Python27\lib\site-packages\tweepy\cursor.py", line 99, in next
data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
File "C:\Program Files Extra\Python27\lib\site-packages\tweepy\binder.py", line 230, in _call
return method.execute()
File "C:\Program Files Extra\Python27\lib\site-packages\tweepy\binder.py", line 203, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: {"errors":[{"message":"Rate limit exceeded","code":88}]}
[Finished in 1937.2s with exit code 1]

这是我的代码:

import tweepy
import time
import csv

ckey = ""
csecret = ""
atoken = ""
asecret = ""

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
    'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

searchTerms = '"good book"'

counter = 0
for tweet in tweepy.Cursor(api.search, q=searchTerms, 
    since='2014-09-15', until='2014-09-16').items(999999999): # changeable here

    try:
        '''print "Name:", tweet.author.name.encode('utf8')
        print "Screen-name:", tweet.author.screen_name.encode('utf8')
        print "Tweet created:", tweet.created_at'''

        placeHolder = []
        placeHolder.append(tweet.author.name.encode('utf8'))
        placeHolder.append(tweet.author.screen_name.encode('utf8'))
        placeHolder.append(tweet.created_at)

        with open("TweetData_goodBook_15SEP2014_all.csv", "ab") as f: # changeable here
            writeFile = csv.writer(f)
            writeFile.writerow(placeHolder)

        counter += 1

        if counter == 4000:
            time.sleep(60*20) # wait for 20 min everytime 4,000 tweets are extracted 
            counter == 0
            continue

    except tweepy.TweepError:
        time.sleep(60*20)
        continue

    except IOError:
        time.sleep(60*2.5)
        continue

    except StopIteration:
        break
4

0 回答 0