简体   繁体   English

tweepy 获取两个日期之间的推文

[英]tweepy get tweets between two dates

I have the following code in Python:我在 Python 中有以下代码:

import tweepy

consumer_key = "..."
consumer_secret = "..."

access_token = "..."
access_token_secret = "..."

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

start_date = datetime.datetime(2018, 1, 19, 12, 00, 00)
end_date = datetime.datetime(2018, 1, 19, 13, 00, 00)

api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.user_timeline, screen_name="@IBM", since=start_date, until=end_date).items():
    print("ID TWEET: " + str(tweet.id))

Is there a way to get tweets between start_date and end_date , by modifying the cursor with tweepy?有没有办法通过用 tweepy 修改 cursor 来获取start_dateend_date之间的推文?

I have already tried to use the since= and until= parameters, but they have not worked.我已经尝试过使用since=until=参数,但它们没有起作用。

Thank you in advance.先感谢您。

First of all the Twitter API does not allow to search by time.首先,Twitter API 不允许按时间搜索。 Trivially, what you can do is fetching tweets and looking at their timestamps afterwards in Python, but that is highly inefficient.简单地说,你可以做的是在 Python 中获取推文并查看它们的时间戳,但这是非常低效的。

You can do that by the following code snippet.您可以通过以下代码片段来做到这一点。

consumerKey = "CONSUMER_KEY"
consumerSecret = "CONSUMER_SECRET"
accessToken = "ACCESS_TOKEN"
accessTokenSecret = "ACCESS_TOKEN_SECRET"

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)

api = tweepy.API(auth)

username = sys.argv[1]
startDate = datetime.datetime(2011, 6, 1, 0, 0, 0)
endDate =   datetime.datetime(2012, 1, 1, 0, 0, 0)

tweets = []
tmpTweets = api.user_timeline(username)
for tweet in tmpTweets:
    if tweet.created_at < endDate and tweet.created_at > startDate:
        tweets.append(tweet)

while (tmpTweets[-1].created_at > startDate):
    tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
    for tweet in tmpTweets:
        if tweet.created_at < endDate and tweet.created_at > startDate:
            tweets.append(tweet)

Although highly inefficient.虽然效率极低。 It works, can helped me in creating my own bot.它有效,可以帮助我创建自己的机器人。

I've just used until (optional operator) and it seems to work pretty well.我刚刚使用了直到(可选运算符),它似乎工作得很好。 I used it like this:我是这样用的:

tweets = tw.Cursor(api.search,
                   q=search_words,
                   lang="en",
                   since=date_since,
                   until=date_until,
                   result_type="recent"
                   ).items(2)

Inspired by @papaya answer here, this works for me, for multiple hashtags query受@papaya 答案的启发,这对我有用,适用于多个主题标签查询

startDate = utc.localize(startDate) 
endDate = utc.localize(endDate)   

tweets = []
tmpTweets = api.search_tweets('hashtags and filteration')

for tweet in tmpTweets:
    if tweet.created_at < endDate and tweet.created_at > startDate:
        tweets.append(tweet)

while (tmpTweets[-1].created_at > startDate):
    tmpTweets = api.search_tweets(new_search, max_id = tmpTweets[-1].id)
    for tweet in tmpTweets:
        if tweet.created_at < endDate and tweet.created_at > startDate:
            tweets.append(tweet)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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