问题
I am trying to figure out how to output the location of the twitter user only if they have it displayed. How would I go about doing that? Right now I have this:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
from HTMLParser import HTMLParser
ckey = ''
csecret = ''
atoken = ''
asecret = ''
class listener(StreamListener):
def on_status(self, status):
print status.text
if status.coordinates:
print 'coords:', status.coordinates
if status.place:
print 'place:', status.place.full_name
return True
on_event = on_status
def on_error(self, status):
print status
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["twerk"])
Edit: it's giving an error for the last line of code. How could I filter the word "twerk" or "miley"
So it's currently outputting a tweet if the tweet consists of the words twerk or miley, but I would like to get the coordinates of that tweet only if they have it displayed. I thought it would be something like tweet = data.coordinates, but that is not working. Any ideas?
回答1:
Don't use string manipulation when you can just load the JSON as a Python object, using json.loads():
import json
from HTMLParser import HTMLParser
def on_data(self, data):
data = json.loads(HTMLParser().unescape(data))
tweet = data['text']
print tweet
return True
This also gives you access to the other fields of the Tweet object, such as the coordinates:
if data['coordinates']:
print data['coordinates']
or the place object:
if data.get('place'):
print data['place']['full_name']
For the streaming API, you may want to not override the on_data()
method and instead use the on_event()
or on_status()
handlers; the default on_data()
implementation loads the JSON and passes on a parsed Tweepy object to those handlers:
class listener(StreamListener):
def on_status(self, status):
print status.text
if status.coordinates:
print 'coords:', status.coordinates
if status.place:
print 'place:', status.place.full_name
return True
on_event = on_status
def on_error(self, status):
print status
I see messages like:
Ainda sonho com uma apresentação de twerk ao vivo #8BieberManiaNaZonaLivreFM #MTVHottest Justin Bieber
coords: {u'type': u'Point', u'coordinates': [-49.319543, -16.679431]}
place: Goiânia, Goiás
Fly by with the above listener.
来源:https://stackoverflow.com/questions/25224692/getting-the-location-using-tweepy