我想使用 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 消息,因为它需要访问的本地显示器来控制浏览器。
替代方案:
可以考虑使用以下替代方案:
-
Twilio API: Twilio 提供了一个强大的 WhatsApp Business API,允许以编程方式发送和接收消息。可以在 Colab 中安装 Twilio Python 库,并使用的 Twilio 帐户凭据发送 WhatsApp 消息。
-
Vonage API: 与 Twilio 类似,Vonage 也提供了一个 WhatsApp API,可以将其集成到的 Colab 项目中。
-
其他基于云的 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