我正在尝试使用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之间)。 我尝试使用此查询,但它什么都不返回:
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
我该怎么做感谢
答案 0 :(得分:3)
这可能不是最好的方法,但它对我有用。
我的方法是首先获取当前日期,然后在查询中使用它
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