Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Implement Tweets Live Streaming Using Tweepy and Save Tweets in .csv file

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.01k
    Answer it

    I am a newbie to python,  after reading streaming with Tweepy and going through this example I am just trying to write tweepy app to crawl  live stream data with the tweepy Api and save it to .csv file but I  got stuck  with tweepy stream Api , when I run my code, its not returning the stream tweet either I don’t get any error, it open a file and write the column name  on the csv file , I can’t figure out where is the problem with my code  my code look like this:- 

    import sys
    import tweepy
    import csv
    import json
    import time
    
    consumer_key=""
    consumer_secret=""
    access_key =""
    access_secret =""
    #use variables to access twitter
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)
    class CustomStreamListener(tweepy.StreamListener):
       
    
        def on_data(self,data):
            if data:
                tweet_json = json.loads(data)
                if tweet_json:
                    if not tweet_json['text'].strip().startswith('RT '):
    Created = data.created_at.strftime("%Y-%m-%d %H:%M:%S") 
                        Text = data.text.encode('utf8') 
                        Location = data.location('utf8')
                        Follower = data.Number_of_follwers('utf8')
                        Name = data.author.screen_name('utf8')
                        Friend = data.friends_count('utf8')
                        with open('OutputStreaming.csv', 'a') as f:
                            writer = csv.writer(f)
                            writer.writerow([Created, Text ,Loaction ,Follower ,Name ,Friend, status.entities.get('hashtags')])
                            Time.sleep(10)
            return True
    
    
        def on_error(self, status_code):
            if status_code == 420:
                return False
            else:
                print >> sys.stderr, 'Encountered error with status code:', status_code
            #return False # Don't kill the stream
    
        def on_timeout(self):
            print >> sys.stderr, 'Timeout...'
            return True # Don't kill the stream
    
    # Writing csv titles
    with open('OutputStreaming.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['Date', 'Text', 'Location','Number_Follower', 'User_Name', 'Friends_count','Hash_Tag'])
    
    if __name__ == '__main__':
        l = CustomStreamListener()
        streamingAPI = tweepy.streaming.Stream(api.auth, l)
        streamingAPI.filter(track=['#KFC','#Macdonald'])

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: