是否可以使用JavaScript或Python访问WhatsApp消息数据库历史记录?

xeufq47z  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(57)

我正试着用whatsapp web获取我的whatsapp账户的聊天记录,其实我可以用selenium来逐个聊天,提取每一个对话(基本上是抓取)。
有没有办法访问存储这些消息的数据库,这样我就不需要刮它了?我已经查了一些库,比如whatsapp-web.js,但是我找不到任何只给我消息的库。

lymnna71

lymnna711#

对于Selenium,您需要首先安装最新的Chromedriver作为webdriver,您可以在此处下载https://sites.google.com/a/chromium.org/chromedriver/downloads
然后我们安装Seleniumpip install selenium
下面可能是一个Python脚本,所有的信息都在注解中,你必须用你想要的变量替换正确的变量。

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Replace "your_phone_number" with the phone number of the chat you want to scrape.
target_chat = "your_phone_number"

# Initialize the webdriver
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com")

# Wait for QR code to be scanned
wait = WebDriverWait(driver, 60)
element = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="side"]')))

# Search for the target chat
search_bar = driver.find_element_by_xpath('//div[@class="_2_1wd"]')
search_bar.click()
search_bar.send_keys(target_chat)
search_bar.send_keys(Keys.ENTER)

# Wait for chat to open
wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="_1ays2"]')))

# Scroll up to load more messages (increase the range to load more messages)
for _ in range(5):
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
    time.sleep(2)

# Get all message elements
messages = driver.find_elements_by_xpath('//div[contains(@class, "message-in") or contains(@class, "message-out")]')

# Extract the text from the message elements
for message in messages:
    try:
        text_element = message.find_element_by_css_selector("span.selectable-text")
        print(text_element.text)
    except:
        continue

# Close the browser
driver.quit()

相关问题