从硒导入网络驱动程序
driver = webdriver.Chrome()
login_url = 'http://www.attheraces.com/racecard/Wolverhampton/6-October-2018/1715'
driver.get(login_url)
html = driver.execute_script("返回 document.documentElement.outerHTML")
sel_soup = BeautifulSoup(html, 'html.parser')
print(sel_soup.findAll("sectionals-time"))
当我运行脚本的最后一行时,它只是返回
[]
据我所知,这是一个动态网站,因此当您访问该网站并向下滚动到结果时,单击“分段时间”选项卡,然后右键单击第一个列出的马的第一个分段时间并进行检查。然后,这向我显示了“分段时间”的类属性,因此我很难理解为什么它没有为马匹生成分段时间。 非常感谢任何建议和帮助。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
url = 'http://www.attheraces.com/racecard/Wolverhampton/6-October-2018/1715'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(2)
driver.find_element_by_xpath('//*[@id="racecard-tabs-1061960"]/div[1]/div/div[1]/ul/li[2]/a').click()
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="tab-racecard-sectional-times"]/div/div[1]/div[1]/div[2]/div/button')))
# method 1
for horse in driver.find_elements_by_class_name('card-item'):
horseName = horse.find_element_by_class_name('form-link').text
times = horse.find_elements_by_class_name('sectionals-time')
times = [time.text for time in times]
print('{}: {}'.format(horseName, times))
print()
# method 2
for horse in driver.find_elements_by_class_name('card-item'):
for time in horse.find_elements_by_class_name('sectionals-time'):
print(time.text)
print()
driver.close()
soup.findAll("span", {"class": "sectionals-time"})
希望有帮助
Web Scraping API来简化您的数据提取过程并轻松获得您需要的结果。