使用 tweepy 在时间间隔内查询推文

Query for Tweet within time interval using tweepy

我正在尝试使用 tweepy 查询特定时间间隔内的推文。

将下面的代码片段用于天数间隔有效:

page_count = 0
for tweets in tweepy.Cursor(api.search,q=query,count=100,result_type="recent",include_entities=True,since= "2016-02-18", until= "2016-03-18" ).pages():
    page_count+=1
    print tweets[0].text.encode('utf-8')
    if page_count >=20:
        break

但我希望它在时间间隔内,例如(在 06:00 和 13:00 之间)。 我尝试使用此查询,但它 returns 什么都没有:

for tweets in tweepy.Cursor(api.search,q=query,count=100,result_type="recent",include_entities=True,since= "2016-03-18 05:30", until= "2016-03-18 08:30" ).pages():
    page_count+=1
    print tweets[0].text.encode('utf-8')
    if page_count >=20:
        break

我该怎么做。谢谢

这可能不是最好的方法,但对我有用。

我的方法是先获取当前日期,然后在查询中使用它

currentTime = str(datetime.datetime.now().date())
for tweets in tweepy.Cursor(api.search,q=query,count=1,result_type="recent",include_entities=True,since = currentTime).pages():
    tweetTime = tweets[0].created_at # get the current time of the tweet
    now = datetime.datetime.now()
    interval = now - tweetTime # subtract tweetTime from currentTime
    if interval.seconds <= 3900: #get interval in seconds and use your time constraint in seconds (mine is 1hr and 5 mins = 3900secs)
            print tweets[0].text.encode('utf-8')
            print(tweets[0].created_at)
        else:
            shouldContinue = False
            print(interval.seconds)
            print(tweets[0].created_at)

        print('\n')

        if not shouldContinue: # check if tweet is still within time range. Tweet returned are ordered according to recent already.
            print('exiting the loop')
            break