Thursday, April 7, 2022

Webscraping Twitter using a Keyword

 This demo program shows how to retrieve tweets using a keyword and specify the date range.


The output:



The code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import snscrape.modules.twitter as sntwitter
import pandas as pd

# Creating list to append tweet data to
tweets_list2 = []

# Using TwitterSearchScraper to scrape data and append tweets to list
for i,tweet in enumerate(sntwitter.TwitterSearchScraper('COVID Vaccine since:2021-01-01 until:2021-05-31').get_items()):
    if i>5000:
        break
    tweets_list2.append([tweet.date, tweet.id, tweet.content, tweet.user.username])
    
# Creating a dataframe from the tweets list above
tweets_df2 = pd.DataFrame(tweets_list2, columns=['Datetime', 'Tweet Id', 'Text', 'Username'])

print(tweets_df2.head(100))

No comments:

Post a Comment