selenium 如何使用python读取来自联系人的WhatsApp消息?

egmofgnx  于 2022-11-10  发布在  Python
关注(0)|答案(5)|浏览(223)

我正在构建一个机器人,登录到缩放在指定的时间和链接是从WhatsApp获得的。所以我想知道是否有可能直接从WhatsApp中检索这些链接,而不是必须将其复制粘贴到Python中。谷歌充斥着发送消息的指南,但有没有办法阅读和检索这些消息,然后对其进行处理?

wgx48brx

wgx48brx1#

您最多只能尝试使用Selenium WebDriver使用Python阅读WhatsApp消息,因为我非常怀疑您是否可以访问WhatsApp API。
Selify基本上是一个自动化工具,它允许您在浏览器中自动执行任务,因此,或许您可以使用Selify编写一个Python脚本,该脚本自动打开WhatsApp并解析有关您的WhatsApp Web客户端的HTML信息。
首先,我们提到了Selence,但我们只会使用它来自动化WhatsApp的打开和关闭,现在我们必须找到一种方法来读取WhatsApp客户端内部的内容,而这就是Web抓取的魔力来了。
Web抓取是从网站提取数据的过程,在这种情况下,数据由您需要自动获取的Zoom链接表示,而网站是您的WhatsApp客户端。要执行此过程,您需要一种从网站提取(解析)信息的方法,为此,我建议您使用Beautiful Soup,但我建议您至少需要了解HTML的工作原理。
抱歉,如果这可能不能完全回答您的问题,但这是我对这个特定主题的所有知识。

hfwmuf9z

hfwmuf9z2#

您可以在浏览器上使用https://selenium-python.readthedocs.io/在Python中打开WhatsApp。
Selify基本上是一个自动化工具,它允许您在浏览器中自动执行任务,因此,或许您可以使用Selify编写一个Python脚本,该脚本自动打开WhatsApp并解析有关您的WhatsApp Web客户端的HTML信息。
我从“https://towardsdatascience.com/complete-beginners-guide-to-processing-whatsapp-data-with-python-781c156b5f0b”这个网站学习和使用代码。浏览上面提到的链接上写的详细信息。
您必须从这个链接-“https://pypi.org/project/whatsapp-web/”安装外部的python库“WhatsApp-web”。只需输入命令提示符/Windows终端,输入“python-m pip install WhatsApp-web”。
它会显示出结果

python-m pip安装WhatsApp-web

收集WhatsApp-Web
下载WhatsApp_web-0.0.1-py3-one-any.whl(21 KB)
安装收集的程序包:WhatsApp-web
已成功安装WhatsApp-Web-0.0.1

3qpi33ja

3qpi33ja3#

你可以从WhatsApp web上读取所有的cookie,并将它们添加到头文件中,然后使用Requests模块,或者你也可以在其中使用SelSelum。

xnifntxz

xnifntxz4#

更新:

请使用WhatsApp Web中的检查元素节,将每个节的XPath类名从WhatsApp Web的当前时间类名更改为使用以下代码。因为WhatsApp已经更改了元素的类名。

我在使用python创建WhatsApp机器人时曾尝试过这一点。但还是有很多错误,因为我也是初学者。
基于我的研究的步骤:
1.使用Selify WebDriver打开浏览器
1.使用二维码登录WhatsApp
1.如果您知道您将从哪个号码收到会议链接,则使用此步骤,否则请检查此过程之后提到的以下过程。

  • 找到并打开您要在其中接收缩放会议链接的聊天室。

从已知聊天室获取消息执行操作


# 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

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

当您不知道会从谁那里收到缩放会议链接时。


# 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’是用来打开网站的驱动程序

示例:

from selenium import webdriver
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp.com")

# 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.com")
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
  • 如果要复制,请确保缩进在代码块中相等。
  • 可以在下面的链接中阅读我的另一个答案,以了解有关使用Python的WhatsApp Web的更多信息。
  • 使用Python发送的WhatsApp消息中的换行符
  • 我正在使用Python开发WhatsApp机器人。
  • 投稿请联系:anurag.cse016@gmail.com
  • 如果这个答案对你有帮助,请在我的https://github.com/4NUR46上给我一个星。
rn0zuynd

rn0zuynd5#

试试这个,有点麻烦,但可能行得通

import pyautogui
import pyperclip
import webbrowser

grouporcontact = pyautogui.locateOnScreen("#group/contact", confidence=.6) # Take a snip of the group or contact name/profile photo
link = pyperclip.paste()

def searchforgroup():
     global link
     time.sleep(5)
     webbrowser.open("https://web.whatsapp.com")
     time.sleep(30)#for you to scan the qr code if u have done it then u can edit it to like 10 or anything
     grouporcontact = pyautogui.locateOnScreen("#group/contact", confidence=.6)
     x = grouporcontact[0]
     y = grouporcontact[1]
     if grouporcontact == None:
        #Do any other option in my case i just gave it my usual link as
        link = "mymeetlink"
     else:
         pyautogui.moveTo(x,y, duration=1)
         pyautogui.click() 

# end of searching group

def findlink():
    global link
    meetlink = pyautogui.locateOnScreen("#", confidence=.6)#just take another snap of a meet link without the code after the "/"
    f = meetlink[0]
    v = meetlink[1]
    if meetlink == None:
        #Do any other option in my case i just gave it my usual link as
        link = "mymeetlink"
   else:
       pyautogui.moveTo(f,v, duration=.6)
       pyautogui.rightClick()
       pyautogui.moveRel(0,0, duration=2) # You Have to play with this it basically is considered by your screen size so just edit that and edit it till it reaches the "Copy Link Address"
       pyautogui.click()
       link = pyperclip.paste()
       webbrowser.open(link) # to test it out

所以现在你必须安装PYAutogui、PYPERCLIP,然后按照代码片段中的注解进行操作,一切都应该正常工作了:)

相关问题