【发布时间】:2021-01-08 19:46:31
【问题描述】:
我正在尝试通过来自特定用户的 tweepy 流通过流获取实时推文数据,但是我发现从发布推文的确切时间戳和来自我的 tweepy 程序的打印文本的时间戳之间恰好有 4 秒的延迟.这是正常/预期的还是有办法让我的代码更有效率?谢谢!
# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
"""
Class for streaming and processing live tweets.
"""
def __init__(self):
pass
def stream_tweets(self):
# This handles Twitter authetification and the connection to Twitter Streaming API
listener = TweetListener()
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
# This line filter Twitter Streams to capture data by the keywords:
stream.filter(follow=['user_id'])
# # # # TWITTER STREAM LISTENER # # # #
class TweetListener(StreamListener):
#This is a basic listener that just prints received tweets
#Only returns the tweets of given user
def on_status(self, status):
if status.user.id_str != 'user_id':
return
print(status.text)
def on_data(self, data):
try:
json_load = json.loads(data)
text = json_load['text']
if 'RT @' not in text:
print(text)
print(datetime.now())
return True
except BaseException as e:
print("Error on_data %s" % str(e))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
streamer=TwitterStreamer()
streamer.stream_tweets()
【问题讨论】:
标签: python twitter tweepy tweets