我正在使用Python 2.7和tweepy来下载推特数据。但大约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后我得到的回溯的屏幕截图是:
答案 0 :(得分:1)
出于某种原因,某处存在连接问题。你可能想以某种方式处理它。例如,通过重新连接来处理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()