The Wayback Machine - https://web.archive.org/web/20250114045651/https://www.geeksforgeeks.org/tweet-using-python/
Open In App

Tweet using Python

Last Updated : 21 Nov, 2017
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Twitter is an online news and social networking service where users post and interact with messages. These posts are known as “tweets”. Twitter is known as the social media site for robots. We can use Python for posting the tweets without even opening the website. There is a Python library which is used for accessing the Python API, known as tweepy. Here, we are going to use tweepy for doing the same.
Tweepy is not the native library. You need to install it before using it. Installation is easy when you have pip. In the Terminal or Command Prompt, type the following command to install tweepy.

sudo install pip tweepy

In case you don’t have pip, install it as an external library.
Don’t forget to change the change the application type from “Read only” to “Read and write” in the settings. This given the permission to tweet as well.
After this, go to https://apps.twitter.com/. This is used to create a link between your script and Twitter. In “Keys and Access Tokens” tab, get the Consumer Key (API Key), Consumer Secret (API secret), Access Token and Access Token Secret.

Posting a simple tweet




# importing the module
import tweepy
  
# personal details
consumer_key ="xxxxxxxxxxxxxxxx"
consumer_secret ="xxxxxxxxxxxxxxxx"
access_token ="xxxxxxxxxxxxxxxx"
access_token_secret ="xxxxxxxxxxxxxxxx"
  
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
  
# update the status
api.update_status(status ="Hello Everyone !")


This is the simple method which will set the tweet “Hello Everyone!”. This is an easy process and doesn’t hold much any importance in real life. It is integrated into bigger programs for some useful work. We can use for loop to tweet a large number of tweets. For maintaining the time period between any two tweets in the loop, we can use sleep() function from the time module as shown.




time.sleep(600) # waits for 600 seconds


This was all about posting a text tweet. What if we want to post a tweet with a media file, there is a separate method for that.

Posting a tweet with a media file

Sometimes, a user wants to post a tweet with a media file which is quite simple if we use the website interface. Posting with the help of Python takes some effort. It is same as posting the text-only tweet with just two lines of code addition.




# importing the module
import tweepy
  
# personal information
consumer_key ="xxxxxxxxxxxxxxxx"
consumer_secret ="xxxxxxxxxxxxxxxx"
access_token ="xxxxxxxxxxxxxxxx"
access_token_secret ="xxxxxxxxxxxxxxxx"
  
# authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
   
api = tweepy.API(auth)
tweet ="Text part of the tweet" # toDo
image_path ="path of the image" # toDo
  
# to attach the media file
status = api.update_with_media(image_path, tweet) 
api.update_status(status = tweet)


Please don’t forget to change the Application type as stated above. Without changing it, we can’t have the access to post.


Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg