如何使用python从联系人读取whatsapp消息?

编程入门 行业动态 更新时间:2024-10-16 20:19:55
本文介绍了如何使用python从联系人读取whatsapp消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在构建一个在指定时间登录缩放的机器人,并且正在从 whatsapp 获取链接.所以我想知道是否可以直接从 whatsapp 检索这些链接,而不必将其复制粘贴到 python 中.Google 充满了发送消息的指南,但有没有办法读取和检索这些消息,然后对其进行操作?

I'm building a bot that logs into zoom at specified times and the links are being obtained from whatsapp. So i was wondering if it is was possible to retrieve those links from whatsapp directly instead of having to copy paste it into python. Google is filled with guides to send messages but is there any way to READ and RETRIEVE those messages and then manipulate it?

推荐答案

更新:

请使用WhatsApp web中的inspect element section从WhatsApp web的当前时间类名更改每个部分的xpath的类名,以使用以下代码.因为 WhatsApp 已经更改了其元素的类名.

我已经尝试过使用 python 创建一个 WhatsApp 机器人.但是因为我也是初学者,所以还是有很多bug.

Update :

Please change the xpath's class name of each section from the current time class name of WhatsApp web by using inspect element section in WhatsApp web to use the following code. Because WhatsApp have changed its element's class names.

I have tried that in creating a WhatsApp bot using python. But there are still many bugs because of I am also beginner.

基于我的研究的步骤:

使用 selenium webdriver 打开浏览器使用二维码登录 WhatsApp如果您知道将从哪个号码收到会议链接,请使用此步骤,否则请检查此过程后提及的以下过程.

找到并打开您要接收缩放会议链接的聊天室.

#user_name = "Name of meeting link Sender as in your contact list"
Example :
user_name = "Anurag Kushwaha"
#In above variable at place of `Anurag Kushwaha` pass Name or number of Your Teacher
# who going to sent you zoom meeting link same as you have in your contact list.
user = webdriver.find_element_by_xpath('//span[@title="{}"]'.format(user_name))
user.click()
# For getting message to perform action 
message = webdriver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']") 
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing received text message of any chat room.

for i in message:
    try:
        if "zoom.us" in str(i.text):
            # Here you can use you code to preform action according to your need
            print("Perform Your Action")
     except:
        pass

如果您不知道通过哪个号码收到链接.然后您可以获取任何未读联系人块的 div 类,并打开包含该未读 div 类的所有聊天室列表.然后查看打开聊天的所有未读消息,并从div类中获取消息.

当您不知道从谁那里收到 Zoom 会议链接时.

# For getting unread chats you can use
unread_chats = webdriver.find_elements_by_xpath("// span[@class='_38M1B']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing the number of unread message showing the contact card inside a green circle before opening the chat room.

# Open each chat using loop and read message.
for chat in unread_chats:
    chat.click()

    # For getting message to perform action
    message = webdriver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']")
    # In the above line Change the xpath's class name from the current time class name by inspecting span element
    # which containing received text message of any chat room.
    for i in messge:
        try:
            if "zoom.us" in str(i.text):
                # Here you can use you code to preform action according to your need
                print("Perform Your Action")
         except:
             pass

注意:在上面的代码中,webdriver"是您打开 web.whatsapp 的驱动程序

示例:

from selenium import webdriver
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp")
# This wendriver variable is used in above code.
# If you have used any other name then please rename in my code or you can assign your variable in that code variable name as following line.
webdriver = your_webdriver_variable

完整的代码参考示例:


from selenium import webdriver
import time
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp")
time.sleep(25) # For scan the qr code
# Plese make sure that you have done the qr code scan successful.
confirm = int(input("Press 1 to proceed if sucessfully login or press 0 for retry : "))
if confirm == 1:
   print("Continuing...")
elif confirm == 0:
   webdriver.close()
   exit()
else:
   print("Sorry Please Try again")
   webdriver.close()
   exit()
while True:
    unread_chats = webdriver.find_elements_by_xpath("// span[@class='_38M1B']")
    # In the above line Change the xpath's class name from the current time class name by inspecting span element
    # which containing the number of unread message showing the contact card inside a green circle before opening the chat room.
    
    # Open each chat using loop and read message.
    for chat in unread_chats:
        chat.click()
        time.sleep(2)
        # For getting message to perform action
        message = webdriver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']")
        # In the above line Change the xpath's class name from the current time class name by inspecting span element
        # which containing received text message of any chat room.
        for i in messge:
            try:
                if "zoom.us" in str(i.text):
                    # Here you can use you code to preform action according to your need
                    print("Perform Your Action")
             except:
                pass

如果您要复制代码块,请确保缩进相同.

Please make sure that the indentation is equal in code blocks if you are copying it.

可以在以下链接中阅读我的另一个答案,了解有关使用 python 的 WhatsApp 网络的更多信息.

Can read my another answer in following link for more info about WhatsApp web using python.

发送的 WhatsApp 消息中的换行符使用 Python

我正在使用 python 开发 WhatsApp 机器人.

I am developing WhatsApp bot using python.

如需投稿,您可以联系:anurag.cs​​e016@gmail

For contribution you can contact at : anurag.cse016@gmail

请在我的 https://github/4NUR46 上给一颗星 如果这个答案帮助你.

Please give a star on my https://github/4NUR46 If this Answer helps you.

这篇关于如何使用python从联系人读取whatsapp消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 03:57:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   联系人   消息   python   whatsapp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!