I am using Python 2.7 and tweepy for downloading twitter data. But after about 1 MB, the download stops.
My code is as below:
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'])
I run it with the command python tweet.py > twitter_data.text
Also the screen shot of the traceback I get after around 1.4 MB is downloaded is:
Well there's a connection problem somewhere for some reason. You may want to deal with it somehow. For example by handling the ProtocolError
by reconnecting.
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()
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.