我正在使用tweepy来查找包含某个单词的推文,但我想在最后五分钟内获得最新的推文。我该怎么做?这是我目前的代码。
import tweepy
consumer_key = "**********"
consumer_secret = "**********"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token("**********", "**********")
api = tweepy.API(auth)
public_tweets = api.search(q = "", since = "2015-09-26", language = "EN")
for tweet in public_tweets:
print(tweet.text)
答案 0 :(得分:0)
首先:我编辑了你的帖子以删除你的凭据,我建议你从twitter获得新的,永远不要再分享它们。
同时将您的api.search
(Rest API)更改为Streaming API。当您打开该连接时,这将为您提供符合条件的部分推文。
例如
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
consumer_key = '****'
consumer_secret = '****'
access_token = '****'
access_secret = '****'
class Listener(StreamListener):
def on_status(self, status):
try:
print(str(status.text.encode('utf-8')))
except Exception as e:
print(e)
def on_error(self, status_code):
print(status_code)
while True:
try:
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, Listener())
twitterStream.filter(q=['python'])
except Exception as e:
print(e)