我正在尝试建立一个基于给定关键字列表流式推文的Twitter Bot。
在Twitter上设置Auth时,如下面的代码中所述,我已将wait_on_rate_limit
和wait_on_rate_limit_notify
都指定为True。
tweepy.API(self.auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
但是,当使用此Auth凭据从Tweepy启动流时,它会将wait_on_rate_limit
和wait_on_rate_limit_notify
的值都重置为False。
print(f'Twitter API Client Retry and Wait :',api.api.wait_on_rate_limit)
print(f'Streaming Class Retry and Wait :',myStreamListener.api.wait_on_rate_limit)
print(f'Stream Object Retry and Wait :',myStream.api.wait_on_rate_limit)
Twitter API Client Retry and Wait : True
Streaming Class Retry and Wait : False
Stream Object Retry and Wait : False
如何将这些选项从Tweepy.stream设置为True?
完整代码:
#import re
import tweepy
from tweepy import OAuthHandler
#from textblob import TextBlob
# creating object of TwitterClient Class
class TwitterClient(object):
'''
Generic Twitter Class
'''
def __init__(self):
'''
Class constructor or initialization method.
'''
# keys and tokens from the Twitter Dev Console
consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# attempt authentication
try:
# create OAuthHandler object
self.auth = OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
self.auth.set_access_token(access_token, access_token_secret)
# create tweepy API object to fetch tweets
self.api = tweepy.API(self.auth,wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
except:
print("Error: Authentication Failed")
# Streaming Class
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.user.screen_name)
print('--------------------------------------------------------------')
print(status.text)
print('--------------------------------------------------------------')
def on_error(self, status):
print (status)
# Twitter Client API Auth
api = TwitterClient()
# Initialize Streaming Object
myStreamListener = MyStreamListener()
# Establish a Stream With API and Streaming Object
myStream = tweepy.Stream(auth = api.auth,tweet_mode="extended",
include_rts=False ,listener=myStreamListener)
# Stream Tweets Based on Key Words
myStream.filter(track=['CAA NRC India'], is_async=True)
print(f'Twitter API Client Retry and Wait :',api.api.wait_on_rate_limit)
print(f'Streaming Class Retry and Wait :',myStreamListener.api.wait_on_rate_limit)
print(f'Stream Object Retry and Wait :',myStream.api.wait_on_rate_limit)
myStream.running = False
Twitter API Client Retry and Wait : True
Streaming Class Retry and Wait : False
Stream Object Retry and Wait : False
https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py#L33
在您的情况下:myStreamListener = MyStreamListener(api=api.api)
(这确实使您如何调用TwitterClient
实例api
感到困惑。)
对于Stream,我没有任何东西,因为它似乎总是创建一个新的API()
实例:https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py#L235您可以使用myStream.api = api.api
我很好奇,但是您如何在流式传输时遇到速率限制问题。这不应该发生。这不是您手动反复调用的端点。