首页 > 编程问答 >如何在Google Colaboratory上导入并使用PyWhatKit?

如何在Google Colaboratory上导入并使用PyWhatKit?

时间:2024-07-31 10:55:24浏览次数:13  
标签:python google-colaboratory whatsapp sendmessage pywhatkit

我想使用 pywhatkit 库自动从 Google Colab 在 WhatsApp 上发送消息。到目前为止,我已经尝试过这样做:

pip install pywhatkit

输出:

Requirement already satisfied: pywhatkit in /usr/local/lib/python3.7/dist-packages (5.3)
Requirement already satisfied: wikipedia in /usr/local/lib/python3.7/dist-packages (from pywhatkit) (1.4.0)
Requirement already satisfied: pyautogui in /usr/local/lib/python3.7/dist-packages (from pywhatkit) (0.9.53)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from pywhatkit) (2.23.0)
Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from pywhatkit) (7.1.2)
Requirement already satisfied: mouseinfo in /usr/local/lib/python3.7/dist-packages (from pyautogui->pywhatkit) (0.1.3)
Requirement already satisfied: pyscreeze>=0.1.21 in /usr/local/lib/python3.7/dist-packages (from pyautogui->pywhatkit) (0.1.28)
Requirement already satisfied: pygetwindow>=0.0.5 in /usr/local/lib/python3.7/dist-packages (from pyautogui->pywhatkit) (0.0.9)
Requirement already satisfied: PyTweening>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from pyautogui->pywhatkit) (1.0.4)
Requirement already satisfied: python3-Xlib in /usr/local/lib/python3.7/dist-packages (from pyautogui->pywhatkit) (0.15)
Requirement already satisfied: pymsgbox in /usr/local/lib/python3.7/dist-packages (from pyautogui->pywhatkit) (1.0.9)
Requirement already satisfied: pyrect in /usr/local/lib/python3.7/dist-packages (from pygetwindow>=0.0.5->pyautogui->pywhatkit) (0.2.0)
Requirement already satisfied: pyperclip in /usr/local/lib/python3.7/dist-packages (from mouseinfo->pyautogui->pywhatkit) (1.8.2)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->pywhatkit) (2021.10.8)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->pywhatkit) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->pywhatkit) (2.10)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->pywhatkit) (1.24.3)
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.7/dist-packages (from wikipedia->pywhatkit) (4.6.3)

我还尝试查看 pip list 是否安装了它,它说它有:

pip list

输出:

....
....
pywhatkit     5.3
....
....

当我尝试导入时,它显示错误.

import pywhatkit

输出:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-16-54f6f476dc6e> in <module>()
----> 1 import pywhatkit

4 frames
/usr/lib/python3.7/os.py in __getitem__(self, key)
    679         except KeyError:
    680             # raise KeyError with the original key value
--> 681             raise KeyError(key) from None
    682         return self.decodevalue(value)
    683 

KeyError: 'DISPLAY'

此问题的解决方法或解决方案是什么?


遇到的错误信息 "KeyError: 'DISPLAY'" 表明 PyWhatKit 无法找到系统上的显示服务器。这是因为 PyWhatKit 使用了依赖于图形用户界面 (GUI) 的库,例如 PyAutoGUI,而 Google Colab 是一个云端环境,默认情况下没有 GUI。

解决方案:

不幸的是,无法在 Google Colab 上直接使用 PyWhatKit 发送 WhatsApp 消息,因为它需要访问的本地显示器来控制浏览器。

替代方案:

可以考虑使用以下替代方案:

  1. Twilio API: Twilio 提供了一个强大的 WhatsApp Business API,允许以编程方式发送和接收消息。可以在 Colab 中安装 Twilio Python 库,并使用的 Twilio 帐户凭据发送 WhatsApp 消息。

  2. Vonage API: 与 Twilio 类似,Vonage 也提供了一个 WhatsApp API,可以将其集成到的 Colab 项目中。

  3. 其他基于云的 WhatsApp API: 还有其他一些基于云的 WhatsApp API 提供商,例如 MessageBird、Infobip 等。可以探索这些选项并选择最适合需求的选项。

示例(使用 Twilio):

from twilio.rest import Client

# 的 Twilio 帐户 SID 和身份验证令牌
account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # 替换为的帐户 SID
auth_token = "your_auth_token"  # 替换为的身份验证令牌

client = Client(account_sid, auth_token)

message = client.messages.create(
    body="好,这是来自 Twilio 的 WhatsApp 消息!",  # 的消息内容
    from_="whatsapp:+14155238886",  # 的 Twilio WhatsApp 号码
    to="whatsapp:+1234567890"  # 收件人的 WhatsApp 号码
)

print(message.sid)

请记住,需要注册一个 Twilio 帐户并设置一个 WhatsApp Business 帐户才能使用其 API。

希望这可以帮助到!

标签:python,google-colaboratory,whatsapp,sendmessage,pywhatkit
From: 71637233

相关文章

  • 使用 pywhatkit 发送 Whatsapp 消息但出现错误
    嘿,我是python编程新手,我正在尝试使用Pywhatkit在特定时间向特定号码发送Whatsapp消息。这是我的代码importpywhatkitpywhatkit.sendwhatmsg("anumber","Hi",0,43)这是我在运行此代码时收到的错误|||请帮我解决这个问题。PSD:\PROJECTS\python>pyth......
  • 三种语言实现二维前缀和(C++/Python/Java)
    题目输入一个n行m列的整数矩阵,再输入q个询问,每个询问包含四个整数x1,y1,x2,y2表示一个子矩阵的左上角坐标和右下角坐标。对于每个询问输出子矩阵中所有数的和。输入格式第一行包含三个整数n,m,q接下来n行,每行包含m个整数,表示整数矩阵。接下来q行,每行包含四个整数......
  • Python rocketMq 客户端的同步和异步模式
    同步模式fromrocketmq.clientimportPushConsumer,ConsumeStatusimporttimedefcallback(msg):print(msg.id,msg.body,msg.get_property('property'))returnConsumeStatus.CONSUME_SUCCESSdefstart_consume_message():consumer=PushCon......
  • python中元组的学习
    元组目录元组元组的概念元组操作元组的常用方法元组的遍历元组的概念Tuple(元组)与列表相似,不同之处遭遇元组的元素不能修改元组表示多个元素组成的序列用于储存一串信息,数据之间使用,分隔元组用()定义#元组的创建info_tuple=("zhangsan",18,1.75)info_tuple2=(1,)#......
  • 尝试通过Python访问.zip文件中的.gz文件
    我有一个包含大量.gz文件的.zip文件,我需要对其进行处理。我想打开.zip,我可以通过以下代码轻松完成:zf=zipfile.ZipFile("file.zip","r")forgzfileinzf.filelist:withgzip.GzipFile(fileobj=zf.open(gzfile.filename,"r"),mode="r")asf:df......
  • python导入包报错ImportError: cannot import name ‘Protocol‘
    python32.pyTraceback(mostrecentcalllast):File"2.py",line5,in<module>importptwt#use"fromsrcimportptwt"foraclonedtherepoFile"……lib/python3.6/site-packages/ptwt/_util.py",line2......
  • Python - Creating your own Iterator
    Inourfirstexample,wewillcreateiterableobjects,which,wheniteratedover,willgiveoutcubesofnumbers,andtheseobjectswillsupportmultipleiterations.classCubes:def__init__(self,start,stop):self.start=startsel......
  • 三种语言实现前缀和(C++/Python/Java)
    题目输入一个长度为n的整数序列。接下来再输入m个询问,每个询问输入一对l,r对于每个询问,输出原序列中从第l个数到第r个数的和。输入格式第一行包含两个整数n和m。第二行包含n个整数,表示整数数列。接下来m行,每行包含两个整数l和r,表示一个询问的区间范围。......
  • Python - 旨在通过命令提示符执行数据清理,但代码似乎无法运行
    我从一位同事那里收到了这段代码,我打算用它来处理100csv文件以提取有关粒子的值。代码如下所示:importsysimportcsv#Usage#skdata_decode.py[inputfile1][inputfile2]...#(Itispossibletousefiledcardtospecifyinputfiles.)##l......
  • 如何在 python 终端中的 x,y 位置上书写(基于文本)
    我想在python(基于文本)的终端中的定义位置(x,y)上写入字符。假设,我有一个大小为25x80的终端,并且想要在位置(2,20)上写入字符。我可以在Python中执行此操作吗?现在,我使用25x80数组,并写入该数组。为了在屏幕上显示,我清除屏幕并将该数组的全部内容写入屏幕,但这效......