Query for Tweet within time interval using tweepy

坚强是说给别人听的谎言 提交于 2021-01-27 06:43:03

问题


I am trying to use tweepy to query for tweet that falls within certain interval.

Using the snippet below for days interval works:

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

but i want it to be within time interval e.g (between 06:00 and 13:00). I tried using this query but it returns nothing:

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

How do i do this. Thanks


回答1:


This might not be the best way, but it works for me.

My approach was to first get the current date and then, used it in the query

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


来源:https://stackoverflow.com/questions/36089305/query-for-tweet-within-time-interval-using-tweepy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!