Skip to content

Instantly share code, notes, and snippets.

@AkhilRD
Last active August 11, 2022 08:21
Show Gist options
  • Save AkhilRD/cd0dba447e41c1970e4319736151bc87 to your computer and use it in GitHub Desktop.
Save AkhilRD/cd0dba447e41c1970e4319736151bc87 to your computer and use it in GitHub Desktop.
Scraping tweets with 10 lines of code
# Importing the libraries
import snscrape.modules.twitter as sntwitter
import pandas as pd
# Query and creating an empty list for storing tweets
query = "MSFT Microsoft min_faves:100 lang:en until:2022-06-30 since:2022-01-01"
tweets = []
# A simple for loop
for tweet in sntwitter.TwitterSearchScraper(query).get_items():
tweets.append([tweet.date,tweet.user.username,tweet.content])
# Converting it to a pandas dataframe
df = pd.DataFrame(tweets,columns = ['Date','User','Tweet'])
print(df.head())
# Saving it as a csv
df.to_csv("Microsoft_Tweets.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment