3 回答
TA贡献1829条经验 获得超13个赞
这应该打印所有带有index_score的文章。
art = driver.find_elements_by_xpath("//article[@class='index__contentBlock__7vKo-']//span[@class='index__score__16yy9']")
for i in art:
print(i.text)
TA贡献1789条经验 获得超8个赞
您已经收集了艺术的所有元素。
您所要做的就是:
art = driver.find_elements_by_xpath("//article[@class='index__contentBlock__7vKo-']")
for i in art:
print(i.text)
让我知道这是否有效。
TA贡献1856条经验 获得超17个赞
使用Selenium提取评级,例如2,0Python您必须引发WebDriverWait,并且visibility_of_all_elements_located()
可以使用以下任一定位器策略:
使用CSS_SELECTOR和get_attribute("innerHTML"):
driver.get('https://www.kununu.com/de/volkswagen/kommentare/100')
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[class^='index__ratingBlock'] span[class^='index__score__']")))])
使用XPATH和文本属性:
driver.get('https://www.kununu.com/de/volkswagen/kommentare/100')
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[starts-with(@class, 'index__ratingBlock')]//span[starts-with(@class, 'index__score__')]")))])
控制台输出:
['2,0', '4,5', '3,8', '4,8', '2,8', '4,7', '3,2', '4,0', '4,9', '4,2']
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
添加回答
举报