无法收集任何推文

时间:2014-01-11 19:50:58

标签: twitter tweepy

此代码在不久前工作正常,但现在它无法正常工作?

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import random

consumer_key = ""
consumer_secret = ""
access_token_key = ""
access_token_secret = ""
Coords = dict()
Place = dict()
PlaceCoords = dict()
XY = []


class StdOutListener(StreamListener):
                """ A listener handles tweets that are the received from the stream.
                This is a basic listener that inserts tweets into MySQLdb.
                """

                def on_status(self, status):
                                #print "Tweet Text: ",status.text
                                text = status.text
                                #print "Time Stamp: ",status.created_at
                                try:
                                    Coords.update(status.coordinates)
                                    XY = (Coords.get('coordinates'))  #Place the coordinates values into a list 'XY'
                                    #print "X: ", XY[0]
                                    #print "Y: ", XY[1]
                                except:
                                    #Often times users opt into 'place' which is neighborhood size polygon
                                    #Calculate center of polygon
                                    Place.update(status.place)
                                    PlaceCoords.update(Place['bounding_box'])
                                    Box = PlaceCoords['coordinates'][0]
                                    XY = [(Box[0][0] + Box[2][0])/2, (Box[0][1] + Box[2][1])/2]
                                    #print "X: ", XY[0]
                                    #print "Y: ", XY[1]
                                    pass
                                # Comment out next 4 lines to avoid MySQLdb to simply read stream at console
                                #print {"status_id":status.id_str,"timestamp":status.created_at,"location X":XY[0],"location Y":XY[1],"text":text}

                                print status.id_str,status.created_at,XY[0],XY[1],text


def main():
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token_key, access_token_secret)
    stream = Stream(auth, l, timeout=30)
    #sleep
    nsecs = 2

    #Only records 'locations' OR 'tracks', NOT 'tracks (keywords) with locations'
    while True:
        try:
            # Call tweepy's userstream method
            #stream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41],languages=['es','tr','ko','fr','ru','de','ja','it','pt'], async=False)##These coordinates are approximate bounding box around USA
            stream.filter(locations=[-180,-90,180,90],async=False)
            #stream.filter()
            #stream.filter(track=['obama'])## This will feed the stream all mentions of 'keyword'
            break
        except Exception, e:
            print Exception , e
            # Abnormal exit: Reconnect
            #nsecs=random.randint(30)
            print "Reconnecting ",nsecs
            time.sleep(nsecs)

if __name__ == '__main__':
    main()

有没有其他方法可以根据位置收集流媒体数据?

1 个答案:

答案 0 :(得分:1)

不,使用locations参数是唯一的方法。但是,我强烈建议不要在except中反对你的所有StdOutListener声明。它最有可能捕获一种不同于你期望的错误,掩盖问题。