通过 Streaming 下载 Twitter 数据时出错 API

getting error in downloading twitter data through Streaming API

我正在使用 Python 2.7 和 tweepy 下载 Twitter 数据。但是大约 1 MB 后,下载停止。

我的代码如下:

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


#consumer key, consumer secret, access token, access secret.
ckey="NlilpJMYf7i4dGYXtJJONrDt5"
csecret="S1xIaTJs2M1S4Okfm3LF1JkjGkNBOaDpkNl6HayOPcLFLWMrak"
atoken="1144571546-MnpLF9GQqVdo5QzBysL1iZFW0p49kixER1xkHvK"
asecret="LnVOgnFlgIaVd0qBYf7bJ9Dzl0nE2oKrjBDYc0L69XEsH"



class listener(StreamListener):

   def on_data(self, data):
    print(data)
    return(True)

   def on_error(self, status):
    print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track= ['nepal','police','army','constitution','earthquake','government','minister','kathmandu post','janakpur','everest'])

我 运行 使用命令 python tweet.py > twitter_data.text

此外,我在下载大约 1.4 MB 后获得的回溯屏幕截图是:

嗯,由于某种原因某处出现连接问题。您可能想以某种方式处理它。例如,通过重新连接来处理 ProtocolError

from requests.packages.urllib3.exceptions import ProtocolError
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener


CKEY = 'NlilpJMYf7i4dGYXtJJONrDt5'
CSECRET = 'S1xIaTJs2M1S4Okfm3LF1JkjGkNBOaDpkNl6HayOPcLFLWMrak'
ATOKEN = '1144571546-MnpLF9GQqVdo5QzBysL1iZFW0p49kixER1xkHvK'
ASECRET = 'LnVOgnFlgIaVd0qBYf7bJ9Dzl0nE2oKrjBDYc0L69XEsH'


class Listener(StreamListener):

    @staticmethod
    def on_data(data):
        print(data)
        return True

    @staticmethod
    def on_error(status):
        print(status)


def main():
    auth_handler = OAuthHandler(CKEY, CSECRET)
    auth_handler.set_access_token(ATOKEN, ASECRET)

    while True:
        try:
            stream = Stream(auth_handler, Listener())
            stream.filter(
                track=[
                    'nepal', 'police', 'army', 'constitution', 'earthquake',
                    'government', 'minister', 'kathmandu post', 'janakpur',
                    'everest',
                ]
            )
        except ProtocolError as error:
            print(error)


if __name__ == '__main__':
    main()