我想刮掉所有的元素从什么是应用程序,但我有问题的硒python这是我的代码:
from selenium import webdriver
from selenium.webdriver.common import keys
import time
driver = webdriver.Chrome("path/to/chromedriver")
driver.get("https://web.whatsapp.com/")
time.sleep(10)
input("Qr Code: ")
driver.implicitly_wait(10)
numbers = driver.find_elements_by_class_name('_ccCW')
for n in numbers:
print(n.text)
和我的脚本只抓取17个项目和whatsapp有3个类_ccCW
FqYAR
i0jNr
和每个类有一个有17个项目,所以如何抓取这三个类
WhatsApp的相同元素可以使用2/3不同的xpath访问,因此对元素使用单个xpath将不会总是返回文本。
一些chrome扩展将有助于识别xpath很好。我使用了以下4个扩展。
- HTML DOM导航
- TruePath
- XPath助手 xPath查询
以下是whatsapp
的固定元素chat_search_box = "/html/body/div[1]/div[1]/div[1]/div[3]/div/div[1]/div/label/div/div[2]"
selected_profile_header_name = "//HEADER[@class='_23P3O']//SPAN[@class='_ccCW FqYAR i0jNr']"
chat_name = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/header/div[2]/div[1]/div/span"
footer = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/footer/div[1]"
footer_textbox = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/footer/div[1]/div[2]/div/div[1]/div/div[2]"
msg_nav_arrow = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/div[3]/div/div[1]/span/div/span[2]"
xpath下面的用于访问左窗格中的聊天名称。这些是固定的,但div号是动态的。I =div #聊天组
pane_base = "(//div[@id='pane-side']//div[@class='_3OvU8'])"
pane_search_parent = lambda x : pane_base + "[" + str(x) + "]"
pane_group_last_sender = lambda i: pane_search_parent(i) + "//span[@class='FqYAR i0jNr']"
pane_user_sms = lambda i : pane_search_parent(i) + "//span[@class='_ccCW FqYAR i0jNr']"
pane_sms_date = lambda i: pane_search_parent(i) + "//div[@class='_3vPI2']/div[@class='_1i_wG']"
pane_notif = lambda i: pane_search_parent(i) + "//span[@class='_23LrM']"
pane_username = lambda i: pane_search_parent(i) + "//div[@class='zoWT4']"
用于从聊天中访问消息的是最难的部分,并且相同的元素有多个xpath。
msg_base = "(//div[@id='main']//div[@class='y8WcF']/div)[2]" #u have to change [2] with [n] to read specific msg
#u could find SENDER from any of 3 xpath depending on msg type.. sometime msg could be quoted text or first post or forwared msg or could be with image.
SENDER = [msg_base + "/div/div/div/div[1]/span[1]", msg_base + "//span[@class='a71At _3xSVM i0jNr']", msg_base + "//span[@class='_1BUvv']"]
#following item follows same as SENDER. please add 'msg_base' to each list items.
SENDER_NAME = ['/div/div/div/div[1]/span[2]']
SENDER_TEXT = ['/descendant::div/span/span[1]','//span[@class="i0jNr selectable-text copyable-text"]']
QUOTED_TEXT = ['/div/div/div/div[2]/div[1]/div/div/div/div/div[2]',"//span[@class='quoted-mention i0jNr']",'/div/div/div/div[1]/div[1]/div/div/div/div/div[2]']
QUOTED_SENDER = ['/div/div/div/div[2]/div[1]/div/div/div/div/div[1]/span[1]','/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/span',"//span[@class='a71At i0jNr']"]
TIME = ["//span[@class='kOrB_']",'/descendant::div[last()]']
请随时询问有关使用python selenium抓取whatsapp的任何问题
如果类名是_ccCW
,FqYAR
和i0jNr
,则尝试
classes_to_get = ["_ccCW", "FqYAR", "i0jNr"]
for kls in classes_to_get:
numbers = driver.find_elements_by_class_name(kls)
for n in numbers:
print(n.text)
尝试使用这个xpath:
//div[@role='gridcell']//span[@title]
numbers = driver.find_elements_by_xpath("//div[@role='gridcell']//span[@title]")
for n in numbers:
print(n.text)
#print(n.get_attribute("innerText")) # You can also use this line to print the text.
您可以像下面这样使用这三个元素构建xpath:
//*[contains(@class, '_ccCW') or contains(@class, 'FqYAR') or contains(@class, 'i0jNr')]
代码:
numbers = driver.find_elements_by_xpath("//*[contains(@class, '_ccCW') or contains(@class, 'FqYAR') or contains(@class, 'i0jNr')]")
for n in numbers:
print(n.text)