Change language

Tweet using Python

| |

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.

Post a simple tweet

# module import

import tweepy

 
# personal data

consumer_key = "xxxxxxxxxxxxxxxx"

consumer_secret = "xxxxxxxxxxxxxxxx"

access_token = " xxxxxxxxxxxxxxxx "

access_token_secret = "xxxxxxxx xxxxxxxx "

  
# consumer key and secret authentication

auth = tweepy.OAuthHandler (consumer_key, consumer_secret)

 
# access token and secret authentication
auth.set_access_token (access_token, access_token_secret)

api = tweepy.API (auth)

 
# update status

api.update_status (status = " Hello Everyone! " )

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.

time.sleep ( 600 ) # waits 600 seconds

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.

Post a tweet with a media file

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.

# module import

import tweepy

 
# Personal information * 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" # make

image_path = "path of the image" # make

 
# attach media

status = api.update_with_media (image_path, tweet ) 

api.update_status (status = tweet)

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 . 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.

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