Whatsapp 使用Python !
您是否曾经希望自动祝您的朋友生日快乐,或在预设时间自动向您的朋友(或任何 Whatsapp 联系人!)发送一组消息,或通过在 WhatsApp 上发送数千条随机文本来发送您的朋友!使用浏览器自动化,您可以完成所有这些工作,甚至更多!
首先,您必须安装这些:
1) Selenium的Python绑定(浏览器自动化软件)
pip install selenium
2) Chrome网络驱动程序
从此处下载 Chrome 驱动程序:Chromedriver 下载页面(选择您的特定版本)将其解压到已知位置,因为我们稍后需要该位置
如果您卡在某个地方,请参阅文档:文档链接
3) Chromium Web Browser( chrome浏览器开源版)
sudo apt-get install chromium-browser
就是这样!你都准备好了。
让我们立即潜入
Python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Replace below path with the absolute path
# to chromedriver in your computer
driver = webdriver.Chrome('/home/saket/Downloads/chromedriver')
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)
# Replace 'Friend's Name' with the name of your friend
# or the name of a group
target = '"Friend\'s Name"'
# Replace the below string with your own message
string = "Message sent using Python!!!"
x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((
By.XPATH, x_arg)))
group_title.click()
inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="9"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for i in range(100):
input_box.send_keys(string + Keys.ENTER)
time.sleep(1)
随身携带手机。从 WhatsApp 的顶部栏中选择 WhatsApp 网页(3 个点)
然后运行脚本(确保您已添加 chrome 驱动程序的绝对路径并已将目标变量替换为您朋友的姓名)。扫描屏幕上出现的二维码,享受Python的力量吧!
Please use this script only for educational purposes, i am not responsible if your friends ( or even Whatsapp ) block you.
随意修改代码。尝试 :
- 一次给多个组发短信
- 从预定义的消息列表中随机发送消息或
- 发送完全随机的文本。
在下方评论您的体验!
谈到浏览器自动化,这只是冰山一角。将写更多关于浏览器自动化的文章,让您一睹它的威力!
相关帖子:
使用Selenium 的浏览器自动化