Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.