0

I'm new to python programming and Twitter API. I tired to collect tweets with a hashtag from a specific time period(say 11/24/216-11/27/2017), my goal is to get coordinates from those extracted tweets and save the coordinates and the tweet text into a csv file. But my problem is that i don't know how to set the time filter and save them into a file. What's more, only a few tweets contained the coordinates, was that common? Here are the python scripts that i found online.

import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

#Enter Twitter API Key information
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''

file = open("C:\\Output.csv", "w") #This script didn't work on my Mac#

strong text file.write("X,Y\n")

data_list = []
count = 0

class listener(StreamListener):

def on_data(self, data):
    global count

    #How many tweets you want to find, could change to time based
    if count <= 2000:
        json_data = json.loads(data)

        coords = json_data["coordinates"]
        if coords is not None:
           print coords["coordinates"]
           lon = coords["coordinates"][0]
           lat = coords["coordinates"][1]

           data_list.append(json_data)

           file.write(str(lon) + ",")
           file.write(str(lat) + "\n")

           count += 1
        return True
    else:
        file.close()
        return False

def on_error(self, status):
    print status

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
#What you want to search for here
twitterStream.filter(track=[""])
Ruixue
  • 1
  • 1
  • 1
    Possible duplicate of [How can I obtain a sampling of all tweets from a specific time period?](http://stackoverflow.com/questions/5454509/how-can-i-obtain-a-sampling-of-all-tweets-from-a-specific-time-period) – Karan Nagpal Nov 30 '16 at 16:36
  • Possible duplicate of [Searching of public tweets with hashtag in certain period of time](http://stackoverflow.com/questions/29595714/searching-of-public-tweets-with-hashtag-in-certain-period-of-time) – iCart Nov 30 '16 at 16:41

0 Answers0