简体   繁体   中英

tweets live streaming using tweepy

I am using Tweepy to access the streaming API and storing it in JSON file. The code looks fine but I keep getting this error: AttributeError: 'NoneType' object has no attribute 'strip'

import sys
import tweepy
import json

consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_key = 'XXXX'
access_secret = 'XXXX'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
file = open('today.txt', 'a')

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print (status.text)

    def on_data(self, data):
        json_data = json.loads(data)
        file.write(str(json_data))

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['twitter'])

The error here is in the last line. I think the stream is returning null and so it is giving error when I am trying to filter it. Please help !

Tweepy is no longer being maintained, as of the 18th of October, however this is not the cause of the problem.

The twitter streaming API has been experiencing problems since the attack on DYN the 21st of October. These have been reported on the twitter developer forum, and twitter staff are aware of the issue: https://twittercommunity.com/t/issues-reported-with-streams-since-10-21/76429 .

As of yet, there is not a clear answer for what the cause of the problem is, but the observed behaviour is that connections to any of Twitter's streaming endpoints get closed after short periods of time, normally (although not always) without receiving any data. Sometimes the connection gets closed whilst sending the response resulting in invalid JSON.

I think is a lib problem, i got the same today with my code. I saw that the library is no longer maintained. Probably we have to change library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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