简体   繁体   中英

Python getting all tweets from a hashtag with a specific time

I'm working on a code. It's supposed to take all tweets from a hashtag with a specific time and write it on the terminal or a JSON file or a text file but my program takes only one tweet and writes it for all other tweets. For example:

The output I want:

*A *B *C

The output I get:

*A *A *A

How can I fix this?

Here is my code:

def date_range(start,end):
   current = start
   while (end - current).days >= 0:
      yield current
      current = current + datetime.timedelta(seconds=1)  

class TweetListener(StreamListener):
    def on_status(self, status):
        #api = tweepy.API(auth_handler=auth)
        #status.created_at += timedelta(hours=900)

        startDate = datetime.datetime(2013, 6, 30)
        stopDate = datetime.datetime(2013, 10, 30)
        for date in date_range(startDate,stopDate):
            status.created_at = date
            print ("tweet " + str(status.created_at) +"\n")
            print (status.text + "\n" )
            # You can dump your tweets into Json File, or load it to your database

stream = Stream(auth, TweetListener(), secure=True, )
t = "#twitter" # You can use different hashtags
stream.filter(track=[t])

You cannot stream tweets from past dates. The tweet stream contains only tweets that were tweeted while your stream is open. If you want old tweets use the search/tweets endpoint. However, that will only return tweets from the last week or so. It will not return tweets, as in your example, from 4 years ago. To do that you will need to pay for a service such as Gnip .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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