Getting tweets by date with tweepy

I pulled out the maximum number of tweets allowed from USATODAY, which was 3000.

Now I want to create a script to automatically retrieve USATODAY tweets at 11:59 p.m. of the day of each day.

I was going to use the api stream, but then I have to keep it working all day.

Can I get an idea of ​​how to create a script where it runs the REST API every night at 23:59 to pull out daytime tweets? If no one knows how to pull date-based tweets?

I thought about putting the ifelse statement in my for loop, but that seems inefficient because it will have to search for 3000 tweets every night.

This is what I have now:

client = MongoClient('localhost', 27017)
db = client['twitter_db']
collection = db['usa_collection']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)

api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.user_timeline,id='USATODAY').items():
    collection.insert(tweet._json)
+4
source share
1 answer

. , tweet.created_at, , 1 , , .

import tweepy, datetime, time

def get_tweets(api, username):
    page = 1
    deadend = False
    while True:
        tweets = api.user_timeline(username, page = page)

        for tweet in tweets:
            if (datetime.datetime.now() - tweet.created_at).days < 1:
                #Do processing here:

                print tweet.text.encode("utf-8")
            else:
                deadend = True
                return
        if not deadend:
            page+=1
            time.sleep(500)

get_tweets(api, "anmoluppal366")

. 3000 , , 24 .

+6

Source: https://habr.com/ru/post/1589099/


All Articles