Change language

Retrieving Tweets with Tweepy

|

Steps to get keys:
— Sign in to Twitter for Developers. 
— Go to "Create Application"
— Fill in the application details. 
— Click Create Twitter App. 
— The details of your new application will be displayed along with the consumer key and consumer secret. 
— To get an access token, click Generate My Access Token. The page will refresh and generate an access token.

Tweepy — this is one of the libraries that needs to be installed using pip. Now, in order to authorize our application to access Twitter on our behalf, we need to use the OAuth interface. Tweepy provides a convenient Cursor interface for iterating over different types of objects. Twitter allows a maximum of 3200 tweets to be retrieved.

These are all prerequisites and must be used before receiving tweets from a user.

Code (with explanation):

import tweepy

 
# Populate X with the credentials received
# following the above procedure.

consumer_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"  

consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

access_key = "XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX "

access_secret =

 
# Function to retrieve tweets

def get_tweets (username):

 

# Authorization by consumer key and consumer secret

auth = tweepy.OAuthHandler ( consumer_key, consumer_secret)

 

# Access to user’s access key and access secret

  auth.set_access_token (access_key, access_secret)

 

# API call

api = tweepy.API (auth)

 

# 200 tweets to be retrieved

number_of_tweets = 200

tweets = api.user_timeline (screen_name = username)

 

  # Empty array

tmp = [] 

 

# create an array of tweet information: username,

# tweet id, date / time, text

  tweets_for_csv = [tweet.text for tweet in tweets] # CSV file created

for j in tweets_for_csv:

 

# Add tweets to an empty tmp array

tmp.append (j) 

 

# Print tweets

print (tmp)

 

 
# Driver code

if __ name__ = = ’__main__’ :

 

# Here goes the Twitter handle for the user for

# whose tweets should be retrieved.

get_tweets ( "twitter-handle"

Output:
The above script will generate all the tweets of a specific user and will be added to an empty tmp array. Here Tweepy is presented as a tool for accessing Twitter data in a fairly simple way with Python. There are different types of data that we can collect, with an obvious focus on the tweet object. Once we have gathered some data, the possibilities for analytic applications are endless.

One such use of tweet extraction is to analyze feelings or emotions. User emotions can be derived from tweets by tokenizing each word and applying machine learning algorithms to that data. This detection of emotions or feelings is used all over the world and will be widely used in the future.

This article is provided by Ayush Govil . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically