[英]web scraping HTML table
How could i scrape opening odds on this site https://www.betexplorer.com/soccer/russia/premier-league/arsenal-tula-ufa/IwvO3Q5T/ ?我怎么能在这个网站上刮开赔率https://www.betexplorer.com/soccer/russia/premier-league/arsenal-tula-ufa/IwvO3Q5T/ ?
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")
Proposal the opening home odds:建议开盘主场赔率:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).click()
oid = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).get_attribute("@data-oid")
bid = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).get_attribute("@data-bid")
var = oid+'-'+bid
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(@id,'%s')]/tr[last()]/td[@class='bold']" % var))).text
First we click on the td
element containing the final odd.首先我们点击包含最后奇数的
td
元素。 Then we grab 2 attribute values ( data-oid
and data bid
) from this td element.然后我们从这个 td 元素中获取 2 个属性值(
data-oid
和data bid
)。 We concat these 2 values into a variable.我们将这两个值连接成一个变量。 We use this variable in our last XPath expression to locate the
td
element which contains the opening odd.我们在最后一个 XPath 表达式中使用这个变量来定位包含开盘奇数的
td
元素。
For draw odds and away odds, use the following XPath:对于平局赔率和客场赔率,请使用以下 XPath:
(//td[a[.='bet365']]/following-sibling::td[@data-odd])[2]
(//td[a[.='bet365']]/following-sibling::td[@data-odd])[3]
Add exceptions in case there's no opening odds to get (no opening draw odds for bet365
in your sample webpage).添加例外情况以防无法获得开盘赔率(示例网页中的
bet365
没有开盘平局赔率)。
Imports:进口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.