How to extract twitter data using Twitter API?

Lerekoqholosha
1 min readSep 19, 2021

--

Twitter has an enormous amount of tweets every day, and every tweet has some information which social media marketers using for their analysis.

tweepy is required library for this recipe. Installing tweepy on your computer is a very simple. You simply need to install it using pip.

pip install tweepy

Tweeter Data Collection for Analysis

import tweepyfrom tweepy import OAuthHandler# Your Twittter App Credentialsconsumer_key = "XXXXXXXXXXX"consumer_secret = "YYYYYYYYYYY"access_token = "WWWWWWWWWWWWWWWWWWWWW"access_token_secret = "PPPPPPPPPPPPPPP"# Calling APIauth = tweepy.OAuthHandler(consumer_key, consumer_secret)auth.set_access_token(access_token, access_token_secret)api = tweepy.API(auth)# Provide the keyword you want to pull the data e.g. "Pandas".keyword = "Pandas"# Fetching tweetstweets = api.search(keyword, count=10, lang='en', exclude='retweets',tweet_mode='extended')for item in tweets:print(item)

The `consumer key`, `consumer secret`, `access token` and `access token secret` you can get from Twitter developer portal after login and create your app. The above program on execution pull the top 10 tweets with the keyword Pandas is searched. The API will pull English tweets since the language given is “en” and it will exclude retweets.

Tweepy

Twitter Developers

Thank you for reading.

Please let me know if you have any feedback.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Lerekoqholosha
Lerekoqholosha

Written by Lerekoqholosha

I am a data scientist with 3 years of experience working with Python.

No responses yet

Write a response