title: Tweepy: crawl live streaming tweets and save in to a .csv file
body: <p>After reading <a href="http://docs.tweepy.org/en/v3.4.0/streaming_how_to.html" rel="nofollow noreferrer">streaming with Tweepy</a> and going through this <a href="https://github.com/tweepy/tweepy/blob/master/examples/streaming.py" rel="nofollow noreferrer">example</a>. I tried to write a tweepy app to crawl live stream data with the tweepy Api and save it to .csv file. When I run my code, it returns empty csv file ('OutputStreaming.csv') with column names['Date', 'Text', 'Location','Number_Follower','User_Name', 'Friends_count','Hash_Tag], not the stream tweets. I also tried to do it in <a href="https://stackoverflow.com/questions/38281076/tweepy-streamlistener-to-csv?rq=1">this</a> way also <a href="https://stackoverflow.com/questions/28130124/how-to-save-a-tweepy-twitter-stream-to-a-file">this one</a>, but  I am getting the same out put with my code:- </p>
  
  <pre><code>def on_status(self, status):
  with open('OutputStreaming.csv', 'w') as f:
  f.write(['Author,Date,Text')
  writer = csv.writer(f)
  writer.writerow([status.created_at.strftime("%Y-%m-%d \
                  %H:%M:%S")status.text.encode,
                  status.location,
                  status.Number_of_follwers,
                  status.author.screen_name,
                  status.friends_count])
  </code></pre>
  
  <p>I  got stuck. I can’t figure out where is the problem with the code, my code look like this:-</p>
  
  <pre><code>import tweepy
  from tweepy.streaming import StreamListener
  from tweepy import OAuthHandler
  from tweepy import Stream
  import json #data
  #Variables that contains the user credentials to access Twitter API 
  access_token = "***"
  access_token_secret = "***"
  consumer_key = "***"
  consumer_key_secret = "***"
  auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
  auth.set_access_token(access_token, access_token_secret)
  #setup api
  api = tweepy.API(auth) 
  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 &gt;&gt; sys.stderr, 'Encountered error with status code:',\ 
             status_code
      def on_timeout(self):
          print &gt;&gt; sys.stderr, 'Timeout...'
        return True
  # Writing csv titles
  with open('OutputStreaming.csv', 'a') 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=['#Yoga','#Meditation'])
  </code></pre>