簡體   English   中英

如何使用 python 讀取聯系人的 whatsapp 消息?

[英]How do I read whatsapp messages from a contact using python?

我正在構建一個在指定時間登錄縮放的機器人,並且鏈接是從 whatsapp 獲取的。 所以我想知道是否可以直接從 whatsapp 中檢索這些鏈接,而不必將其復制粘貼到 python 中。 谷歌充滿了發送消息的指南,但有沒有辦法閱讀和檢索這些消息然后操縱它?

您最多可以嘗試使用Selenium WebDriver閱讀帶有 Python 的 WhatsApp 消息,因為我強烈懷疑您是否可以訪問 WhatsApp API。

Selenium is basically an automation tool that lets you automate tasks in your browser so, perhaps, you could write a Python script using Selenium that automatically opens WhatsApp and parses HTML information regarding your WhatsApp web client.

首先,我們提到了 Selenium,但我們只會使用它來自動打開和關閉 WhatsApp,現在我們必須找到一種方法來讀取 WhatsApp 客戶端內部的內容,這就是 Web Scraping 的魔力所在。

Web 抓取是從網站提取數據的過程,在這種情況下,數據由您需要自動獲取的 Zoom 鏈接表示,而 web 站點是您的 WhatsApp 客戶端。 要執行此過程,您需要一種從網站中提取(解析)信息的方法,為此我建議您使用Beautiful Soup ,但我建議您至少需要了解 HTML 的工作原理。

抱歉,如果這可能無法完全回答您的問題,但這是我對這個特定主題的所有知識。

您可以使用 Python 中的https://selenium-python.readthedocs.io/在瀏覽器上打開 WhatsApp。

Selenium is basically an automation tool that lets you automate tasks in your browser so, perhaps, you could write a Python script using Selenium that automatically opens WhatsApp and parses HTML information regarding your WhatsApp web client.

我從“ https://towardsdatascience.com/complete-beginners-guide-to-processing-whatsapp-data-with-python-781c156b5f0b ”這個網站學習和使用代碼。 Go 通過上述鏈接上的詳細信息。

您必須從此鏈接安裝外部 python 庫“ whatsapp-web ”---“ https://pypi.org/project/whatsapp-web/ ”。 只需通過“python -m pip install whatsapp-web”輸入命令提示符/windows 終端。

它會顯示結果---

python -m pip 安裝whatsapp-web

收集whatsapp-web

下載 whatsapp_web-0.0.1-py3-none-any.whl (21 kB)

安裝收集的包:whatsapp-web

成功安裝whatsapp-web-0.0.1

更新:

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. 因為 WhatsApp 已更改其元素的 class 名稱。

我已經嘗試過使用 python 創建一個 WhatsApp 機器人。 但是由於我也是初學者,所以仍然有很多錯誤。

基於我的研究的步驟:

  1. 使用 selenium webdriver 打開瀏覽器
  2. 使用二維碼登錄 WhatsApp
  3. 如果您知道將從哪個號碼收到會議鏈接,請使用此步驟,否則請在此過程之后檢查以下過程提及。
  • 找到並打開您將收到縮放會議鏈接的聊天室。

從已知聊天室獲取消息以執行操作

#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. 如果您不知道您將通過哪個號碼收到鏈接。
  2. 然后,您可以獲得任何未讀聯系人塊的 div class 並打開所有包含該未讀 div class 的聊天室列表。
  3. 然后檢查打開聊天的所有未讀消息,並從 div class 中獲取消息。

當您不知道您將從誰那里收到縮放會議鏈接時。

# 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.com 的驅動程序

例子:

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 打個星。

您可以從 whatsapp web 讀取所有 cookies 並將它們添加到標頭並使用請求模塊,或者您也可以使用 selenium 。

試試這個有點麻煩,但它可能會奏效

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 並按照代碼片段中的注釋進行操作,一切都應該正常工作:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM