Skip to content

Instantly share code, notes, and snippets.

@2color
Created February 18, 2020 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2color/8eda6fd8ef14e56652df3ec6d2b35cf7 to your computer and use it in GitHub Desktop.
Save 2color/8eda6fd8ef14e56652df3ec6d2b35cf7 to your computer and use it in GitHub Desktop.
Extract replies to a tweet into a CSV
import csv
import tweepy
# get credentials at developer.twitter.com
auth = tweepy.OAuthHandler('API Key', 'API Secret')
auth.set_access_token('Access Token', 'Access Token Secret')
api = tweepy.API(auth)
# update these for whatever tweet you want to process replies to
name = 'MylesBorins'
tweet_id = '1227025858700857344'
replies=[]
for tweet in tweepy.Cursor(api.search,q='to:'+name,result_type='recent',timeout=999999).items(1000):
if hasattr(tweet, 'in_reply_to_status_id_str'):
if (tweet.in_reply_to_status_id_str==tweet_id):
replies.append(tweet)
with open('replies_clean.csv', 'w') as f:
csv_writer = csv.DictWriter(f, fieldnames=['user', 'text'])
csv_writer.writeheader()
for tweet in replies:
row = {'user': tweet.user.screen_name, 'text': tweet.text.replace('\n', ' ')}
csv_writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment