Use python streaming and location information to get Tweet


#1

I am a computer science student. I am interested in mining tweets for geographic data in support of my thesis .Now I could use python streaming to get the tweet(use filter). I would like to be able to use the location information of my country to get some tweets.But it was not work.Here is my code(Python). Thanks your advice.

Here is my code.

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

consumer_key =
consumer_secret =
access_token =
access_token_secret =

class StdOutListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.

"""
def on_data(self, data):
    print data
    return True

def on_error(self, status):
    print status

if name == ‘main’:
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

loc = [“135.098,34.650”,“135.299,34.840”]

print access_token

stream = Stream(auth, l)
stream.filter(locations = loc)

#2

You should check the tweepy docs to figure out the right way to pass location. The parameter should be 4 values, comma-separated, so the array of two elements you’re supplying looks like it may be incorrect.


#3

Thank for your reply.I think the parameter of location was wrong.Could you teach me how to set the parameter of the locations? Is it locations[“132.2,29.9,146.2,39.0”,“138.4,33.5,146.1,46.20”]?
I am now in Japan and I try to use the locations=[“132.2,29.9,146.2,39.0”,“138.4,33.5,146.1,46.20”] to get the twitters sent in Japan.
The python program worked but no Japanese twitters.
I am a newcomer of twitter API.
Thank for your help.


#4

Hi, I’m also looking for a way to get the tweet location in python. I’m using twitter-1.10.0 lib. Thanks


#5

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
ckey=‘8GbbVytSENby3WyiNx46w’
csecret=‘VDXCMPpcK579nEZBlOjJ1vQ4hewLPh4U9VZGMNmx8’
atoken=‘2326257572-U99aWkm6pbV92psbsR5gOUrePlj6UMMfbGJ6NbJ’
asecret=‘piMRiR2Zf46H7u1X5uDL9KuIuxb9mBBoVOSAX10zgKqOh ‘
class listener(StreamListener):
def on_data(self, data):
try:
#print data
tweet = data.split(’,“text”:"’)[1].split(’","source’)[0]
print tweet
saveThis = str(time.time())+’::’+tweet
saveFile = open(‘twitDB2.csv’,‘a’)
saveFile.write(saveThis)
saveFile.write("\n")
saveFile.close()
return True
except BaseException, e:
print ‘failed on data’,str(e)
time.sleep(5)
def on_error(self, status) :
print status
auth= OAuthHandler(ckey,csecret)
auth.set_access_token(atoken, asecret)
twitterStream= Stream(auth, listener())
twitterStream.filter(track=[“car”])

This is my code for collecting twitter streaming data using python at first i was able to get the data but from past two weeks i was continously getting 401 error


#6

you should not show your secrets info on public.