Tweepy按日期获取推文

时间:2020-09-07 20:04:13

标签: python twitter tweepy

我希望在日期之间获得有关某个主题的推文。这可能在tweepy吗? (或用于Twitter的其他API?)

我可以通过使用ID将其用于user_timeline,但是当我将其更改为使用api.search时,该程序基本上只会继续运行而没有任何输出

def getTweets(username):
    tweets = []
    tmpTweets = api.user_timeline(username, tweet_mode = 'extended', include_rts=True)
    for tweet in tmpTweets:
        if tweet.created_at < endDate and tweet.created_at > startDate:
            tweets.append(tweet)

        while (tmpTweets[-1].created_at > startDate):
            tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id,tweet_mode = 'extended')
            for tweet in tmpTweets:
                if tweet.created_at < endDate and tweet.created_at > startDate:
                    tweets.append(tweet)
    return tweets

tl; dr: python中是否有一种方法可以基于关键字搜索在两个日期之间获取推文?

1 个答案:

答案 0 :(得分:0)

使用 cursor 怎么样:

text_query = 'Coronavirus'
since_date = '2020-02-10'
until_date = '2020-08-10'
max_tweets = 150

# Creation of query method using parameters
tweets = tweepy.Cursor(api.search,q=text_query, since=since_date, until=until_date).items(max_tweets)

也在此blog和此answer中进行了描述。

相关问题