繁体   English   中英

关于tweepy的最新tweet for python?

[英]Newest tweets on tweepy for python?

我正在使用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)

首先:我编辑了您的帖子以删除您的凭据,建议您从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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM