Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Python script to download all the tweets of a hashtag into a csv
import tweepy
import csv
import pandas as pd
####input your credentials here
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
#####United Airlines
# Open/Create a file to append data
csvFile = open('ua.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,q="#unitedAIRLINES",count=100,
lang="en",
since="2017-04-03").items():
print (tweet.created_at, tweet.text)
csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])
@bellegis

This comment has been minimized.

Show comment Hide comment
@bellegis

bellegis Aug 29, 2017

thank you!

thank you!

@vitospinelli

This comment has been minimized.

Show comment Hide comment
@vitospinelli

vitospinelli Jan 3, 2018

When I run this script on Python 27 (on a windows 10 machine) nothing happens and no error is returned... can you please help?

When I run this script on Python 27 (on a windows 10 machine) nothing happens and no error is returned... can you please help?

@impshum

This comment has been minimized.

Show comment Hide comment
@impshum

impshum Jan 30, 2018

@vitospinelli When you see print(things) not print things wrapped in parentheses you're dealing with python 3. Drop 2 unless you really have to use it. You have 2 years to get used to 3: https://pythonclock.org

impshum commented Jan 30, 2018

@vitospinelli When you see print(things) not print things wrapped in parentheses you're dealing with python 3. Drop 2 unless you really have to use it. You have 2 years to get used to 3: https://pythonclock.org

@sushovan1

This comment has been minimized.

Show comment Hide comment
@sushovan1

sushovan1 Mar 21, 2018

I tried using the same code on 2018-03-21 and was thinking to fetch tweet as old as 2018-02-01 but it was unable to return those many tweets, any idea why?

I tried using the same code on 2018-03-21 and was thinking to fetch tweet as old as 2018-02-01 but it was unable to return those many tweets, any idea why?

@streetratonascooter

This comment has been minimized.

Show comment Hide comment
@streetratonascooter

streetratonascooter Mar 27, 2018

@sushonvan1 the twitter API only lets you go back approximately 2 weeks

@sushonvan1 the twitter API only lets you go back approximately 2 weeks

@shruti18196

This comment has been minimized.

Show comment Hide comment
@shruti18196

shruti18196 May 4, 2018

Thanku very much

Thanku very much

@qrnazyhah

This comment has been minimized.

Show comment Hide comment
@qrnazyhah

qrnazyhah May 10, 2018

i want to fetch tweet as old as 2018-01-01, can you help me please?

i want to fetch tweet as old as 2018-01-01, can you help me please?

@kamalikap

This comment has been minimized.

Show comment Hide comment
@kamalikap

kamalikap May 23, 2018

Hi, I want to extract the hashtags from the tweets and store it into a file. is it possible?

Hi, I want to extract the hashtags from the tweets and store it into a file. is it possible?

@kahiin

This comment has been minimized.

Show comment Hide comment
@kahiin

kahiin May 24, 2018

Hi, i want to save the tweets that i obtain into an array, is it possible?! thanks

kahiin commented May 24, 2018

Hi, i want to save the tweets that i obtain into an array, is it possible?! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment