Reputation: 157
I'm using that Python code using Tweepy Library to retrieve Twitter data for a specific hashtag, but the question is i need to retrieve a specific period, for example, from 30 june2013 till 30 December 2013. How can I do that?
#imports
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
#setting up the keys
consumer_key = '……………….'
consumer_secret = '……………..'
access_token = '……………….'
access_secret = '……………..'
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print (data)
return True
def on_error(self, status):
print (status)
#printing all the tweets to the standard output
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
stream = Stream(auth, TweetListener())
t = u"#سوريا"
stream.filter(track=[t])
Upvotes: 0
Views: 5368
Reputation:
I am still investigating why I couldn't get the same results using tweepy.Cursor(api.search, geocode=.., q=query, until=date )
Maybe it's for this reason. But I could retrieve Twitter data using Tweepy between two dates bty going through these steps.
First, I created a generator of dates, between start Date and end Date.
def date_range(start,end):
current = start
while (end - current).days >= 0:
yield current
current = current + datetime.timedelta(seconds=1) #Based on your need, but you could do it per day/minute/hour
Then, I created a Listener
so I can get the tweets that are created in specific day by accessing to status.created_at
Your Code should look like:
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import datetime
#Use your keys
consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_secret = '...'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
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, 06, 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 = u"#Syria" # You can use different hashtags
stream.filter(track=[t])
Output:
I just printed the dates to check (I don't wanna spam StackOverflow with the political tweets).
tweet 2013-06-30 00:00:01
-------------------
tweet 2013-06-30 00:00:02
-------------------
tweet 2013-06-30 00:00:03
-------------------
tweet 2013-06-30 00:00:04
-------------------
tweet 2013-06-30 00:00:05
-------------------
tweet 2013-06-30 00:00:06
-------------------
tweet 2013-06-30 00:00:07
-------------------
tweet 2013-06-30 00:00:08
-------------------
tweet 2013-06-30 00:00:09
-------------------
Upvotes: 3