Making very specific time requests (to the second) on Twitter API, using Python Tweepy?

天大地大妈咪最大 提交于 2019-11-26 17:07:32

问题


I would like to request tweets on a specific topic (for example: "cancer"), using Python Tweepy. But usually its time can only be specified by a specific day, for example.

startSince = '2014-10-01'
endUntil = '2014-10-02'

for tweet in tweepy.Cursor(api.search, q="cancer", 
    since=startSince, until=endUntil).items(999999999):

Is there a way to specify the time so I can collect "cancer" tweets between 2014-10-01 00:00:00 and 2014-10-02 12:00:00? This is for my academic research: I was able to collect cancer tweets for the last month, but the sudden burst of quantity of "breast cancer" tweets due to the cancer awareness month breaks my script and I have to collect them in different time segments, and I will not be able to retrieve the tweets for Oct 01, 2014 if I can't figure it out soon.


回答1:


There is no way that I've found to specific a time using since/until.

You can hack your way around this using since_id & max_id.

If you can find a tweet made at around the times you want, you can restrict you search to those made after since_id and before max_id

import tweepy

consumer_key        = 'aaa'
consumer_secret     = 'bbb'
access_token        = 'ccc'
access_token_secret = 'ddd'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

results = api.search(q="cancer", since_id=518857118838181000, max_id=518857136202194000)

for result in results:
    print result.text


来源:https://stackoverflow.com/questions/26205102/making-very-specific-time-requests-to-the-second-on-twitter-api-using-python

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