网络抓取HTML表

时间:2020-08-12 14:48:55

标签: python html selenium xpath web-scraping

我如何在此网站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")

1 个答案:

答案 0 :(得分:1)

建议开场赔率:

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

首先,我们单击包含最终奇数的td元素。然后,从该td元素中获取2个属性值(data-oiddata bid)。我们将这两个值合并为一个变量。我们在最后一个XPath表达式中使用此变量来定位包含开头奇数的td元素。

对于平局赔率和远距赔率,请使用以下XPath:

(//td[a[.='bet365']]/following-sibling::td[@data-odd])[2]
(//td[a[.='bet365']]/following-sibling::td[@data-odd])[3]

添加例外情况,以免没有开盘赔率(示例网页中bet365没有开盘赔率)。

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC