【发布时间】:2020-08-12 08:34:42
【问题描述】:
我会在这个页面上刮开开盘赔率https://www.betexplorer.com/soccer/russia/premier-league/results/ 我试过这段代码:
try:
driver.find_element_by_xpath("//td[a[.='bet365']]/following-sibling::td[span]")
except NoSuchElementException:
homeodd = 'no bet365 odd'
drawodd = 'no bet365 odd'
awayodd = 'no bet365 odd'
else:
driver.find_element_by_xpath("//*[@id='sortable-1']/tbody/tr[6]/td[4]").click()
sleep(3)
homeodd = driver.find_element_by_xpath("//table[ends-with(@id,'16')]//tr[th='Opening odds']/following-sibling::tr/td[@class='bold']").text
print(homeodd)
driver.find_element_by_xpath("//*[@id='sortable-1']/tbody/tr[6]/td[5]").click()
sleep(3)
drawodd = driver.find_element_by_xpath("//table[ends-with(@id,'16')]//tr[th='Opening odds']/following-sibling::tr/td[@class='bold']").text
print(drawodd)
driver.find_element_by_xpath("//*[@id='sortable-1']/tbody/tr[6]/td[6]").click()
sleep(3)
awayodd = driver.find_element_by_xpath("//table[ends-with(@id,'16')]//tr[th='Opening odds']/following-sibling::tr/td[@class='bold']").text
print(awayodd)
我有这个错误: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//table[ends-with(@id,'16')]//tr[th='Opening odds' ]/following-sibling::tr/td[@class='bold']' 不是有效的 XPath 表达式。 但是我错误的 xpath 语法。
问题是这个页面没有data-opening-odd属性。我在上一篇文章中询问了另一个 scraping with selenium web driver 并且在社区的一个很好的建议下我找到了这个很好的解决方案
try:
driver.find_element_by_xpath("//td[a[.='bet365']]/following-sibling::td[span]")
except NoSuchElementException:
homeodd = 'no bet365 odd'
drawodd = 'no bet365 odd'
awayodd = 'no bet365 odd'
else:
homeodd = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//td[a[.="bet365"]]/following-sibling::td[span][1]'))).get_attribute("data-opening-odd")
drawodd = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//td[a[.="bet365"]]/following-sibling::td[span][2]'))).get_attribute("data-opening-odd")
awayodd = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//td[a[.="bet365"]]/following-sibling::td[span][3]'))).get_attribute("data-opening-odd")
有什么建议可以在不从属性data-opening-odd 中获取几率的情况下调整第二种解决方案?谢谢
【问题讨论】:
标签: python selenium selenium-webdriver xpath web-scraping