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()
1条答案
按热度按时间lymnna711#
对于
Selenium
,您需要首先安装最新的Chromedriver
作为webdriver,您可以在此处下载https://sites.google.com/a/chromium.org/chromedriver/downloads然后我们安装
Selenium
pip install selenium
下面可能是一个Python脚本,所有的信息都在注解中,你必须用你想要的变量替换正确的变量。