Last active
August 11, 2022 08:21
-
-
Save AkhilRD/cd0dba447e41c1970e4319736151bc87 to your computer and use it in GitHub Desktop.
Scraping tweets with 10 lines of code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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