pywhatkit 模块介绍
Python提供了许多内置库来简化我们的工作。其中pywhatkit是一个Python库,用于在特定时间发送 WhatsApp 消息,它还具有其他几个功能。
以下是 pywhatkit 模块的一些功能:
- 发送 WhatsApp 消息。
- 播放 YouTube 视频。
- 执行 Google 搜索。
- 获取有关特定主题的信息。
pywhatkit 模块也可用于将文本转换为手写文本图像。
在 Python3 中,pywhatkit 模块不会预先安装,因此您可以使用以下命令安装它:
pip install pywhatkit
1. 发送 Whatsapp 消息:
在这里,我们将学习使用 pywhatkit 模块的最简单方法,该模块利用 WhatsApp 网页自动向 WhatsApp 上的任何号码发送消息。但请确保您已在浏览器中登录 WhatsApp。
Syntax: pywhatkit.sendmsg(“receiver mobile number”,”message”,hours,minutes)
Parameters:
- Receiver mobile number: The Receiver’s mobile number must be in string format and the country code must be mentioned before the mobile number.
- Message: Message to be sent(Must be in string format).
- Hours: This module follows the 24 hrs time format.
- Minutes: Mention minutes of the scheduled time for the message(00-59).
例子:
Python3
# importing the module
import pywhatkit
# using Exception Handling to avoid
# unprecedented errors
try:
# sending message to receiver
# using pywhatkit
pywhatkit.sendwhatmsg("+91xxxxxxxxxx",
"Hello from GeeksforGeeks",
22, 28)
print("Successfully Sent!")
except:
# handling exception
# and printing error message
print("An Unexpected Error!")
Python3
# importing the module
import pywhatkit
# using Exception Handling
# to avoid exceptions
try:
# it plays a random YouTube
# video of GeeksforGeeks
pywhatkit.playonyt("GeeksforGeeks")
print("Playing...")
except:
# printing the error message
print("Network Error Occured")
Python3
# importing the module
import pywhatkit
# use Try Except to
# handle the Exceptions
try:
# it will perform the Google search
pywhatkit.search("GeeksforGeeks")
print("Searching...")
except:
# Printing Error Message
print("An unknown error occured")
Python3
# importing the module
import pywhatkit
# using Exception Handling for
# handling unprecendented errors
try:
# it will perform google search
pywhatkit.info("Google", lines = 4)
print("\nSuccessfully Searched")
except:
# printing error message
print("An Unknown Error Occured")
2. 播放 YouTube 视频:
函数pywhatkit.playonyt() ,在您的默认浏览器中打开 YouTube 并播放您在函数中提到的视频。如果您将主题名称作为参数传递,它会播放关于该主题的随机视频。在将视频的 URL 作为参数传递时,它会打开该确切的视频。
Syntax: pywhatkit.playonyt(“url/topic name”)
Parameters:
- URL/Topic Name: Mention the topic name or URL of the YouTube video
例子:
Python3
# importing the module
import pywhatkit
# using Exception Handling
# to avoid exceptions
try:
# it plays a random YouTube
# video of GeeksforGeeks
pywhatkit.playonyt("GeeksforGeeks")
print("Playing...")
except:
# printing the error message
print("Network Error Occured")
输出:
3. 执行谷歌搜索:
您可以使用以下简单命令执行 Google 搜索。它会打开您的浏览器并搜索您在代码中给出的主题。
Syntax: pywhatkit.search(“Keyword”)
Parameters:
- Keyword: It open your browser and performs search operation.
例子:
Python3
# importing the module
import pywhatkit
# use Try Except to
# handle the Exceptions
try:
# it will perform the Google search
pywhatkit.search("GeeksforGeeks")
print("Searching...")
except:
# Printing Error Message
print("An unknown error occured")
输出:
4. 获取特定主题的信息:
我们可以获得有关特定主题的简要信息。我们还可以限制要打印的行数。此外,请确保您正在搜索 Wikipedia 上可用的主题。
Syntax: pywhatkit.info(“topic”,lines=x)
Parameters:
- Topic: Gives the output on the topic mentioned.
- lines: It prints the searched topic in the specified number of lines.
例子:
Python3
# importing the module
import pywhatkit
# using Exception Handling for
# handling unprecendented errors
try:
# it will perform google search
pywhatkit.info("Google", lines = 4)
print("\nSuccessfully Searched")
except:
# printing error message
print("An Unknown Error Occured")
输出:
Google LLC is an American multinational technology company that specializes in Internet-related services and products, which include online advertising technologies, a search engine, cloud computing, software, and hardware. It is considered one of the Big Four technology companies alongside Amazon, Apple and Microsoft.Google was founded in September 1998 by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University in California. Together they own about 14 percent of its shares and control 56 percent of the stockholder voting power through supervoting stock. They incorporated Google as a California privately held company on September 4, 1998, in California.
Successfully Searched