简体   繁体   English

使用 Python Tweepy 的 Twitter Streaming API

[英]Twitter Streaming API with Python Tweepy

I've been playing with the Twitter Streaming API using the Tweepy library.我一直在使用 Tweepy 库玩 Twitter Streaming API。 I started by following my own account and streaming my own tweets as I posted them, which worked fine.我开始关注我自己的帐户,并在发布推文时流式传输我自己的推文,效果很好。

I then attempted to stream a fairly large region's tweets ([30,-85,31,-84]), to which I initially seemed to receive no data.然后我尝试流式传输一个相当大区域的推文 ([30,-85,31,-84]),我最初似乎没有收到任何数据。 I then started receiving 'Location Deletion Notices', or 'scrub_geo' messages, and have only ever received those since.然后,我开始收到“位置删除通知”或“scrub_geo”消息,此后才收到这些消息。 I changed my code back to the previously working follow code, but I continue to receive 'scrub_geo' messages and not statuses from my profile.我将我的代码改回以前工作的关注代码,但我继续收到“scrub_geo”消息,而不是我的个人资料中的状态。

Here's the script I'm using:这是我正在使用的脚本:

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

Your coordinates are reversed.你的坐标颠倒了。 Since we're dealing with GeoJSON always do (long,lat,alt) or (x,y,z)因为我们处理GeoJSON总是做(long,lat,alt)(x,y,z)

So you'll need to provide tallahassee=[-85,30,-84,31] .所以你需要提供tallahassee=[-85,30,-84,31] Always provide longitude first same as you would do (x,y) in math.始终首先提供经度,就像您在数学中所做的(x,y)

There are some places, like google maps, that do latitude first.有些地方,比如谷歌地图,先做纬度。 You just have to be careful as to which proper format you're dealing with.您只需要注意您正在处理的正确格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM