You can pull data from twitter in python using the library “tweepy” and your user credentials.
You need to create an application and get the keys, access tokens for that app. I have listed dummy values in the below code. Please generate your at https://developer.twitter.com/en.
The free version of twitter API access allows you to fetch only last one week tweets, if you need more, then get a premium account.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import tweepy from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt import pandas as pd # Variables that contains the user specific credentials to access Twitter API # Please create yours by going to https://developer.twitter.com/en access_token = "3330087118-dnkNytgjujqLJg2fs0RIldyHKgiZoix8DpEIV1iKXhW" access_token_secret = "BTgKB6tvJ6ssxescB4J5OhC4ssOU2n0AnrVAEf2CRrZcirZ8zuo" consumer_key = "hcuRfSV4aWjp9AjHazSaqaot2HcS" consumer_secret = "vLIArJmqrMWizqHcEslrInNjDweECfjthkEreErAE0nWIDRKBSmkJUE" # Connecting to twitter using user credentials auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) # Searching about any keyword in all the latest tweets from delhi tweets_data=api.search('corona virus',count=100, lang='en', include_rts=False, tweet_mode="extended", geocode="28.7041,77.1025,10km") print ("number of tweets found: "+ str(len(tweets_data))) # Creating an empty data frame to store the tweets tweets = pd.DataFrame() # Extracting the tweet metadata for status in tweets_data: tweets=tweets.append(status._json, ignore_index=True) # Looking at only the tweet texts tweets['full_text'].head(10) |
Sample Output:

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!