使用 Tweepy 获取位置 [英] Getting the location using Tweepy

查看:34
本文介绍了使用 Tweepy 获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何仅在 Twitter 用户显示位置时输出它.我该怎么做?现在我有这个:

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"])

它给出了最后一行代码的错误.我如何过滤twerk"或miley"这个词

it's giving an error for the last line of code. How could I filter the word "twerk" or "miley"

因此,如果推文包含单词 twerk 或 miley,它当前正在输出推文,但我想仅在显示该推文时获取该推文的坐标.我认为它会类似于 tweet = data.coordinates,但这不起作用.有什么想法吗?

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?

推荐答案

当您可以使用 json.loads():

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

这也使您可以访问其他 Tweet 对象的字段,比如坐标:

This also gives you access to the other fields of the Tweet object, such as the coordinates:

if data['coordinates']:
    print data['coordinates']

或地点对象:

if data.get('place'):
    print data['place']['full_name']

对于流 API,您可能希望不要覆盖 on_data() 方法,而是使用 on_event()>on_status() 处理程序;默认的 on_data() 实现加载 JSON 并将解析的 Tweepy 对象传递给这些处理程序:

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

我看到如下消息:

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.

这篇关于使用 Tweepy 获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆