简体   繁体   English

如何使用 Twitter API 和 Tweepy 在 DateTime 之后获取所有推文

[英]How to get all tweets after a DateTime with Twitter API and Tweepy

I currently want to look for a keyword and return all mentions of that word.我目前想寻找一个关键字并返回该词的所有提及。 I only want everything within the last 15 minutes though.不过,我只想要最后 15 分钟内的所有内容。 I currently only know how to send a date and not DateTime.我目前只知道如何发送日期而不是 DateTime。 My code is as follows我的代码如下

def search_hash(api, since_id):
    logger.info("Retrieving tweets")
    search_words = "#keyword"
    date_since = "2021-04-28 13:48:01"
    new_since_id = since_id
    tweetss = tw.Cursor(api.search,
        q=search_words,
        result_type='latest',
        lang="en",
        count = 100,
        since=date_since,
        since_id=since_id).items()
    count = 0
    for tweet in tweetss:
        count = count + 1
        print(tweet.text)
        print(tweet.created_at)
        new_since_id = max(tweet.id, new_since_id)
    print(count)
    return new_since_id

I tried using date_since to be a datetime, but I do not think it is working correctly as it is returning times before my filter.我尝试使用 date_since 作为日期时间,但我认为它不能正常工作,因为它在我的过滤器之前返回时间。 Is their anyway to do a datetime?他们无论如何都要约会吗?

I figured this out by getting the ID of the latest tweet then using that id as the last id that should be checked and having the script run every 15 minutes while updating the last_id each time我通过获取最新推文的 ID 然后使用该 ID 作为应检查的最后一个 ID 并让脚本每 15 分钟运行一次,同时每次更新 last_id 来解决这个问题

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

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