Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a python script to download all the tweets of a hashtag into a csv file.
import tweepy
import csv
import pandas as pd
import sys
# API credentials here
consumer_key = 'INSERT CONSUMER KEY HERE'
consumer_secret = 'INSERT CONSUMER SECRET HERE'
access_token = 'INSERT ACCESS TOKEN HERE'
access_token_secret = 'INSERT ACCESS TOKEN SECRET HERE'
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,wait_on_rate_limit_notify=True)
# Search word/hashtag value
HashValue = ""
# search start date value. the search will start from this date to the current date.
StartDate = ""
# getting the search word/hashtag and date range from user
HashValue = input("Enter the hashtag you want the tweets to be downloaded for: ")
StartDate = input("Enter the start date in this format yyyy-mm-dd: ")
# Open/Create a file to append data
csvFile = open(HashValue+'.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,q=HashValue,count=20,lang="en",since=StartDate, tweet_mode='extended').items():
print (tweet.created_at, tweet.full_text)
csvWriter.writerow([tweet.created_at, tweet.full_text.encode('utf-8')])
print ("Scraping finished and saved to "+HashValue+".csv")
#sys.exit()
@sxshateri

This comment has been minimized.

Copy link
Owner Author

sxshateri commented Jun 13, 2019

This script is written in python 3. Please make sure you have installed Tweepy and Pandas before running the script.

@meylanwongkar

This comment has been minimized.

Copy link

meylanwongkar commented Jun 25, 2019

how can i save the tweets with header using pandas?

@sxshateri

This comment has been minimized.

Copy link
Owner Author

sxshateri commented Jun 25, 2019

how can i save the tweets with header using pandas?

what exactly do you mean by "header"? are you referring to the header row of the csv file?

@sameermahajan101

This comment has been minimized.

Copy link

sameermahajan101 commented Sep 26, 2019

how to filter unwanted characters b'RT ,&amp and many more in the csv file?

@smycek

This comment has been minimized.

Copy link

smycek commented Sep 29, 2019

@sxshateri can i use this script to download tweets whom matches two or more words/hashtags?

@smycek

This comment has been minimized.

Copy link

smycek commented Oct 11, 2019

@sxshateri can i use this script to download tweets whom matches two or more words/hashtags?

In the meantime, I've been able to answer this myself: Any operator who follows the guidelines for Twitter search can be used as input.could answer this by my self: any operators following the twitter search guidlines can be used as input.

@sxshateri

This comment has been minimized.

Copy link
Owner Author

sxshateri commented Nov 25, 2019

@sxshateri can i use this script to download tweets whom matches two or more words/hashtags?

In the meantime, I've been able to answer this myself: Any operator who follows the guidelines for Twitter search can be used as input.could answer this by my self: any operators following the twitter search guidlines can be used as input.

yes. as you mentioned any operator within the twitter search guideline can be used in this script.

@this-is-shashank

This comment has been minimized.

Copy link

this-is-shashank commented Apr 18, 2020

I am unable to download tweets older than 2 weeks. Can anyone help here ?

@anuraglahon16

This comment has been minimized.

Copy link

anuraglahon16 commented May 22, 2020

I am unable to download tweets older than 2 weeks. Can anyone help here ?

R u able to do it?

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.