网络抓取 Tennis24 的比赛统计数据

Web scraping Tennis24 in play stats

我一直在努力弄清楚如何在 Tennis 24“https://www.tennis24.com/match/4xFaW6fP/#match-statistics;0”这样的页面上抓取实时和更新统计数据但是当我尝试使用 selenium 时,return 什么也没有。即使我只是尝试 return 第 1 个元素,例如

<div class="statText statText--awayValue">4</div>

有人能给我一些建议吗,因为这是我的第一个抓取项目?

要打印文本 4 你需要引入 for the visibility_of_element_located() and you can use either of the following :

  • 使用 XPATHtext 属性:

    driver.get('https://www.tennis24.com/match/4xFaW6fP/#match-statistics;0')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='statText statText--titleValue' and text()='Aces']//following::div"))).text)
    
  • 使用 XPATHget_attribute('innerHTML'):

    driver.get('https://www.tennis24.com/match/4xFaW6fP/#match-statistics;0')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='statText statText--titleValue' and text()='Aces']//following::div"))).get_attribute('innerHTML'))
    
  • 注意:您必须添加以下导入:

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