【问题标题】:Twitter Streaming API with Python Tweepy使用 Python Tweepy 的 Twitter 流 API
【发布时间】:2015-12-31 15:23:30
【问题描述】:

我一直在使用 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>")

【问题讨论】:

    标签: python twitter location tweepy twitter-streaming-api


    【解决方案1】:

    你的坐标被颠倒了。因为我们正在处理GeoJSON,所以总是使用(long,lat,alt)(x,y,z)

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

    有些地方,比如谷歌地图,先做纬度。你只需要小心你正在处理的正确格式。

    【讨论】:

    • 首先,感谢您对坐标的更正。使用这些坐标发出请求允许我流式传输推文,并且我不再收到“scrub_geo”请求。即使在我不再请求基于位置的推文之后,我仍然对为什么收到“scrub_geo”请求感到有些困惑,但既然它现在可以工作了,我很高兴。
    • 不客气。您还在使用没有坐标的locations=tallahassee 吗?你得到了scrub_geo
    • 不,该行已被注释掉,就像在上面的代码中一样。我在接收scrub_geo 时使用了follow=&lt;user_id&gt; 参数。
    • 也许你在运行它的时候没有偶然评论它。除此之外,我不完全确定。你还能复制吗? follow=&lt;user_id&gt; 也代表一个人的id 对吗?
    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2012-09-17
    • 1970-01-01
    • 2016-06-26
    • 2012-06-13
    相关资源
    最近更新 更多