简体   繁体   English

Tweepy 按日期获取推文

[英]Tweepy get tweets by date

I want to get tweets for a certain topic between dates.我想在日期之间获取某个主题的推文。 Is this possible in tweepy?这在 tweepy 中可能吗? (or any other API for twitter?) (或任何其他用于 twitter 的 API?)

I can get it working for user_timeline by using the IDs, but when I change it to use api.search the program basically just keeps on running without any output我可以通过使用 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: Is there a way in python to get tweets between two dates based on keyword search? tl; dr: python 中有没有办法根据关键字搜索在两个日期之间获取推文?

What about using a 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)

Also described in this blog and this answer .在此博客和此答案中也有描述。

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

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