Introduction to the pywhatkit Library
In this tutorial, we will learn about Python's pywhatkit module. As we know, Python provides numerous libraries and pywhatkit is one of them. The pywhatkit module is used to send the message by the Python script. Using this module, we can send the message to the desired number with a few lines of code. It uses WhatsApp web to send these messages.
Pywhatkit is a Python package for Whatsapp messenger written in Python 3.4+. It is quite simple and sophisticated.
Features of pywhatkit Library
The features of the pywhatkit library are given below.
Installation of Library
We need to install locally to work with the pywhatkit library because it will not come pre-installed in Python3. We can install it using the following command.
pip install pywhatkit
Sending WhatsApp Message
In this section, we will send the message to the Whatsapp automatically in simple way. Here one point is to remember that, to utilize WhatsApp in your browser, we must have an active login session and have previously set up WhatsApp Web in our preferred browser.
The pywhatkit library provides the sendmsg() method to send message automatically. Below is the syntax of sendmsg() method.
Let's understand the following code.
In the above code snippet, we import the pywhatkit library and use the sendwhatmsg() function to send a message to a phone number at a certain hour and minute. The code also uses exception handling to catch and display any errors that may occur while sending the message.
We can also send image with the caption as below -
We can send messages to the group using the following code.
We use this to send messages instantly in a group as if we write 0 hours, 0 minutes then it will send the message at 12:00 AM.
Play a YouTube Video
Python's pywhatkit provides the playont() method which is used to open the YouTube in our default browser and plays video as we passed in the argument. If we don't pass the topic name as parameter, it will play the random video. Otherwise, it opens same video as we pass URL in argument. Let's understand its syntax.
Let's understand the following example -
In the above code, we import the pywhatkit library and use the playonyt() function to play a random YouTube video based on the topic provided as a parameter. It also uses exception handling to catch and display any errors that may occur while playing the video, such as a network error.
Perform the Google Search
We can also use it for the google search using the search() method. It opens our browser and searches for the topic that we have provided in the code. Let's understand the following example.
In the above code, we use the PyWhatKit module to perform a Google search for the website "javatpoint.com". The try-except block is used to handle any errors that may occur during the execution of the search function. If an error occurs, the except block will print an error message "An unknown error occurred".
In summary, this tutorial has covered using the PyWhatKit library in Python to perform automatic WhatsApp messaging. The example code demonstrated the use of the try-except block to handle any potential errors that may occur during the execution of the search function. The tutorial hopes that the reader has learned about using the PyWhatKit library and is ready to implement it in their code.
Next TopicLee Algorithm in Python
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week