Skip to content

Instantly share code, notes, and snippets.

@keithrozario
Created April 22, 2018 11:05
Show Gist options
  • Save keithrozario/bc1ee53f921a8e167827669a518c3d60 to your computer and use it in GitHub Desktop.
Save keithrozario/bc1ee53f921a8e167827669a518c3d60 to your computer and use it in GitHub Desktop.
Search for a Users Tweet on twitter
import tweepy
from custom_config import get_attr_from_list
from custom_config import consumer_key, consumer_secret, access_key, access_secret
def write_tweets_to_file(screen_name):
ids = []
x = 1
with open(screen_name.strip() + '.csv', 'w') as file:
file.write("created_at|url|author|hashtags|user_mentions|text\n")
for tweet in tweepy.Cursor(api.user_timeline, screen_name=screen_name).items():
line = ('%s | %s | %s | %s | %s | %s' % (tweet.created_at,
'https://twitter.com/statuses/' + str(tweet.id),
'@' + tweet.author.screen_name,
get_attr_from_list(tweet.entities['hashtags'], 'text'),
get_attr_from_list(tweet.entities['user_mentions'],
'screen_name'),
tweet.text.replace('\n', "") # don't want new lines on csv
))
ids.append((tweet.id, tweet.created_at))
print("%d. %s" % (x, line))
file.write(line + "\n")
x += 1
return ids
if __name__ == '__main__':
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
with open('screen_names.txt', 'r') as input:
for screen_name in input:
write_tweets_to_file(screen_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment