我正在尝试从比赛网站上抓取数据,但抓取器没有返回任何结果

问题描述 投票:0回答:3

从硒导入网络驱动程序

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"))

当我运行脚本的最后一行时,它只是返回

[]
据我所知,这是一个动态网站,因此当您访问该网站并向下滚动到结果时,单击“分段时间”选项卡,然后右键单击第一个列出的马的第一个分段时间并进行检查。然后,这向我显示了“分段时间”的类属性,因此我很难理解为什么它没有为马匹生成分段时间。

非常感谢任何建议和帮助。

python html selenium web-scraping
3个回答
0
投票
这会起作用。如果您需要不同的输出,请发表评论。

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()
    

0
投票
在我看来你的选择器是错误的, 您是否应该指定:

soup.findAll("span", {"class": "sectionals-time"})
希望有帮助


0
投票
正在为从 At The Races 网站抓取数据而烦恼吗?听起来您当前的刮刀无法切割它。考虑使用强大的

Web Scraping API来简化您的数据提取过程并轻松获得您需要的结果。

© www.soinside.com 2019 - 2024. All rights reserved.