我正在尝试获取前一天的所有推文。为了解决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