简体   繁体   中英

how to display tweets of particular time period using twitter api

my requirement is to display tweets of particular time period using twitter api. I am using getsearch method of api to get all tweets i have tried the following code to display tweets of particular time period.

Three_days_ago = datetime.datetime.utcnow()-datetime.timedelta(days = 3)
for tweet in tweets:
    if tweet.created_at > Three_days_ago:
        print tweet

but i am getting error 'can't compare datetime.datetime to unicode' How to do this please suggest me..

You can use dateutil .

from dateutil import parser


Three_days_ago = datetime.datetime.utcnow()-datetime.timedelta(days = 3)
for tweet in tweets:
    tweeted_datetime = parser.parse(tweet.created_at)
    if tweeted_datetime > Three_days_ago:
        print tweet, tweeted_datetime.strftime("%Y-%m-%d %H:%M:%S %p")

I'm not sure that you'll be able to get the milliseconds as it's not passed while parsing. For more strftime options refer here .

As @jamylak says, tweet.created_at is text not a datetime.

You can convert it to a datetime according to the rules given here: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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