[英]how to display tweets of particular time period using twitter api
my requirement is to display tweets of particular time period using twitter api. 我的要求是使用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.
我正在使用api的getsearch方法来获取所有推文,我尝试了以下代码来显示特定时间段的推文。
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.. 但我收到错误消息
'can't compare datetime.datetime to unicode'
如何执行此操作,建议我..
You can use dateutil . 您可以使用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 . 有关更多
strftime
选项,请参见此处 。
As @jamylak says, tweet.created_at is text not a datetime. 正如@jamylak所说,tweet.created_at是文本而不是日期时间。
You can convert it to a datetime according to the rules given here: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior 您可以根据此处提供的规则将其转换为日期时间: http : //docs.python.org/2/library/datetime.html#strftime-strptime-behavior
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.