Twitter — is an online news and social networking service where users post and interact with them. These posts are known as “tweets”. Twitter is known as a social networking site for robots. We can use Python to post tweets without even opening the site. There is a Python library that is used to access the Python API known as tweepy. Here we will use tweepy for the same.
Tweepy is not a native library. You need to install it before using. Installation is easy when you have a pip. In a terminal or command line, enter the following command to install tweepy.
sudo install pip tweepy
If you don`t have pip, install it as an external library.
Do not forget to change the application type from “Read only” to “Read and write” in the settings. This also gave permission to tweet.
Then go to https://apps.twitter.com/ . This is used to create a link between your script and Twitter. In the Keys and Access Tokens tab, get the consumer key (API key), consumer secret (API secret), access token, and access token secret.
This is a simple method that will set the "Hello everyone" tweet. This is a simple process and doesn`t really matter in real life. It is integrated into large programs for useful work. We can use a loop to tweet a large number of tweets. To maintain a period of time between any two tweets in a loop, we can use the sleep () function from the time module as shown.
It 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.
Sometimes a user wants to post a tweet with a media file, which is pretty simple, if we use the interface of the website. Publishing with Python takes some effort. This is the same as posting just a text tweet with two lines of code.
Please remember to change the application type as stated above. Without changing it, we will not be able to access the publication.
This article is provided by Rishabh Bansal strong> . 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`d like to share more information on the topic discussed above.