首页 > 解决方案 > 终端无数据打印

问题描述

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://www.earningswhispers.com")

element_calendar = driver.find_element_by_id("calendar")
element_calendar.click()

data = driver.find_elements_by_class_name('cor amc showconf nwh')

for d in data:
    title = d.find_element_by_xpath('./html/body/form/div[3]/section/ul/li[26]/div[1]/div[3]').text
    estimate1 = d.find_element_by_xpath('./html/body/form/div[3]/section/ul/li[26]/div[1]/div[6]').text
    estimate2 = d.find_element_by_xpath('./html/body/form/div[3]/section/ul/li[26]/div[1]/div[7]').text
    print(title, estimate1, estimate2)


assert "No results found." not in driver.page_source
driver.quit()

标签: pythonseleniumweb-scraping

解决方案


看起来您的所有定位器都不正确。尝试以下操作:

data = driver.find_elements_by_css_selector('li.cor.amc.showconf.nwh')


for d in data:
    title = d.find_element_by_xpath('./div/div[3]').text
    estimate1 = d.find_element_by_xpath('./div/div[6]').text
    estimate2 = d.find_element_by_xpath('./div/div[7]').text
    print(title, estimate1, estimate2)

印刷:

CRM $0.88 $5.89 B
ULTA $1.92 $1.62 B
BOX $0.17 $200.48 M
GPS ($0.02) $3.40 B
OLLI $0.66 $420.52 M
DELL $1.62 $23.36 B
GES ($0.20) $497.31 M
DOMO ($0.43) $57.49 M
FANH Rev: $0.25
LGF.A ($0.17) -

仅供参考,通常在名称中有空格时查找元素class name是行不通的。在这种情况下使用会更好css selector

此外,当您循环遍历一组元素时,您不会在每个循环中一直返回到根 HTML 元素,您的根从您正在循环的元素开始。


推荐阅读