whatsapp chat scraping

sadadd

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

import time

URL = "https://web.whatsapp.com"

browser = Chrome()
browser.get( URL )

time.sleep(50)
invalid_contacts = ['a']

while True:
    browser.find_element_by_xpath( ".//button[contains(@class, '_1x_c3 _35Ob4 kQJNA _2-5II _338za')]").click()
    time.sleep(20)
    messages = browser.find_elements_by_xpath("//div[contains(@class,'vW7d1')]")
    print("---------------------------------------------------------------")
    for message in messages:        
        try:
            message_text=""
            emojis = []

            message_container = message.find_element_by_xpath(
                ".//div[@class='copyable-text']")

            message_text = message_container.find_element_by_xpath(
                ".//span[contains(@class,'selectable-text invisible-space copyable-text')]"
            ).text
            print("..................................")
            print(message.get_attribute("class"), "MSG:", message_text)

            for emoji in message_container.find_elements_by_xpath(
                    ".//img[contains(@class,'selectable-text invisible-space copyable-text')]"
            ):
                emojis.append(emoji.get_attribute("data-plain-text"))

        except NoSuchElementException:  # In case there are only emojis in the message
            try:
                message_text=""
                emojis = []
                message_container = message.find_element_by_xpath(
                    ".//div[@class='copyable-text']")

                for emoji in message_container.find_elements_by_xpath(
                        ".//img[contains(@class,'selectable-text invisible-space copyable-text')]"
                ):
                    emojis.append(emoji.get_attribute("data-plain-text"))
            except NoSuchElementException:
                pass

Leave a Reply