title: How to get the replies for a given tweet with tweepy and python?
body: <p>After some searching I found a way to download the recent tweets from a given user.   Now I want to get the replies for each tweet. I know that Twitter API does not provide an endpoint to get replies of a tweet unless we have a premium account. But  I could find some workarounds in the internet. I found a way to get few tweets and their replies using <a href="https://stackoverflow.com/questions/29928638/getting-tweet-replies-to-a-particular-tweet-from-a-particular-user">Getting tweet replies to a particular tweet from a particular user</a>. This code is also given below. </p>
  
  <p><strong>How can I modify my code (getData.py) to save the replies of each tweet along with the tweet in the csv?</strong></p>
  
  <blockquote>
    <p>My code to download a user's tweets as csv (getData.py)</p>
  </blockquote>
  
  <pre><code>import tweepy
  import csv
  
  # Twitter API credentials
  consumer_key = "###########"
  consumer_secret = "################"
  access_key = "#################"
  access_secret = "#####################"
  
  
  def get_all_tweets(screen_name):
      # Twitter only allows access to a users most recent 3240 tweets with this method
  
      # authorize twitter, initialize tweepy
      auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
      auth.set_access_token(access_key, access_secret)
      api = tweepy.API(auth)
  
      # initialize a list to hold all the tweepy Tweets
      alltweets = []
  
      # make initial request for most recent tweets (200 is the maximum allowed count)
      new_tweets = api.user_timeline(screen_name=screen_name, count=200)
  
      # save most recent tweets
      alltweets.extend(new_tweets)
  
      # save the id of the oldest tweet less one
      oldest = alltweets[-1].id - 1
  
      # keep grabbing tweets until there are no tweets left to grab
      while len(new_tweets) &gt; 0:
          print
          "getting tweets before %s" % (oldest)
  
          # all subsiquent requests use the max_id param to prevent duplicates
          new_tweets = api.user_timeline(screen_name=screen_name, count=200, max_id=oldest)
  
          # save most recent tweets
          alltweets.extend(new_tweets)
  
          # update the id of the oldest tweet less one
          oldest = alltweets[-1].id - 1
  
          print
          "...%s tweets downloaded so far" % (len(alltweets))
  
      # transform the tweepy tweets into a 2D array that will populate the csv
      outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8"), tweet.favorite_count, tweet.retweet_count]
                   for tweet in alltweets]
  
      # write the csv
      with open('%s_tweets.csv' % screen_name, mode='w', encoding='utf-8') as f:
          writer = csv.writer(f)
          writer.writerow(["id", "created_at", "text"])
          writer.writerows(outtweets)
  
      pass
  
  def main():
      get_all_tweets("tartecosmetics")
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <blockquote>
    <p>How I get the replies for a given tweet</p>
  </blockquote>
  
  <p>This code will fetch 10 recent tweets of an user(name) along with the replies to that particular tweet.</p>
  
  <pre><code>replies=[]
  non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
  for full_tweets in tweepy.Cursor(api.user_timeline,screen_name='tartecosmetics',timeout=999999).items(10):
    for tweet in tweepy.Cursor(api.search,q='to:'+'tartecosmetics',result_type='recent',timeout=999999).items(1000):
      if hasattr(tweet, 'in_reply_to_status_id_str'):
        if (tweet.in_reply_to_status_id_str==full_tweets.id_str):
          replies.append(tweet.text)
    print("Tweet :",full_tweets.text.translate(non_bmp_map))
    for elements in replies:
         print("Replies :",elements)
    replies.clear()
  </code></pre>