python - 使用 Python Tweepy 的 Twitter 流 API

标签 python twitter location tweepy twitter-streaming-api

我一直在使用 Tweepy 库来使用 Twitter Streaming API。我首先关注自己的帐户,并在发布推文时流式传输自己的推文,效果很好。

然后,我尝试传输相当大区域的推文 ([30,-85,31,-84]),最初我似乎没有收到任何数据。然后我开始收到“位置删除通知”或“scrub_geo”消息,并且从那时起就只收到过这些消息。我将代码更改回之前有效的跟随代码,但我继续收到“scrub_geo”消息,而不是我的个人资料中的状态。

这是我正在使用的脚本:

# Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Other libs
import json

# Variables that contains the user credentials to access Twitter API
access_token = "<my_access_token>"
access_token_secret = "<my_secret_token>"
consumer_key = "<my_consumer_key>"
consumer_secret = "<my_consumer_secret>"


# This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        #try:
        #    json_data = json.loads(data)
        #    print json_data['created_at'] + " " + data['text']
        #except:
        print "Data " + str(data)
        return True

    def on_error(self, status):
        print "Error " + str(status)
        if status == 420:
            print("420 error.")
            return False


if __name__ == '__main__':

    # This handles Twitter authetification and the connection to Twitter Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    # Start streaming with right parameters
    #tallahassee=[30,-85,31,-84]
    #stream.filter(locations=tallahassee)           <---- previously used 
    stream.filter(follow="<my_user_id>")

最佳答案

你的坐标颠倒了。由于我们处理 GeoJSON 总是执行 (long,lat,alt)(x,y,z)

因此您需要提供tallahassee=[-85,30,-84,31]。始终首先提供经度,就像数学中的 (x,y) 一样。

有些地方(例如谷歌地图)会首先计算纬度。您只需要小心您正在处理的正确格式即可。

关于python - 使用 Python Tweepy 的 Twitter 流 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32923726/

相关文章:

python - 如何在条形图中从一列列表中绘制词频

java - 位置经理

基于 iPhone 室内位置的应用程序

python - NLTK(python)中如何处理中文?

python - cassandra 创建和使用索引 [pycassa]

objective-c - 只需点击一下即可在 Twitter 上发帖 - 就像 Instagram 一样

android - 如何简单地读取任何 Twitter 用户的推文? (无需登录推特)

python - NUKE 表达式引用顶级相机

python - 如何修复 "TypeError: a float is required"消息?

location - Presence Insights 是否适合准确的位置坐标?