个性化阅读
专注于IT技术分析

自动化:使用Python的Whatsapp!

点击下载

你是否曾经希望自动祝福你的朋友的生日,或发送一组消息给你的朋友(或任何Whastapp联系!)自动在预设的时间,或发送你的朋友发送数以千计的随机文本在whatsapp!使用浏览器自动化,你可以做到所有这些,甚至更多!

首先, 你必须安装以下这些:

1)Selenium的Python绑定(浏览器自动化软件)

pip install selenium

2)Chrome Webdriver

从此处下载Chrome驱动程序:Chromedriver下载页面:https://chromedriver.storage.googleapis.com/index.html?path=2.25/(选择你的特定版本)

将其提取到已知位置, 我们稍后需要这个位置

如果卡在某处, 请参阅文档:文档链接:https://sites.google.com/a/chromium.org/chromedriver/

3)Chromium Web浏览器(chrome浏览器的开源版本)

sudo apt-get install chromium-browser

而已!你们都准备好了。

让我们立刻潜水-

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="input"][@dir="auto"][@data-tab="1"]'
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个点)

屏幕截图2

然后运行脚本(请确保你已为chromedriver添加了绝对路径, 并用你的朋友名称替换了目标变量)。扫描显示在屏幕上的QR码, 享受python的强大功能!

屏幕截图3

请仅将此脚本用于教育目的, 如果你的朋友(甚至Whatsapp)阻止了你, 我概不负责。

随时修改代码。尝试 :

  1. 一次向多个组发送文本
  2. 从预定义的消息列表中随机发送消息, 或
  3. 发送完整的随机文本。

在下面评论你的经历!

在浏览器自动化方面, 这只是冰山一角。将撰写有关浏览器自动化的更多文章, 以使你了解其功能!

相关文章:

使用Selenium的浏览器自动化

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

赞(0)
未经允许不得转载:srcmini » 自动化:使用Python的Whatsapp!

评论 抢沙发

评论前必须登录!