从 Tennis 24 抓取 table

Web scraping a table from Tennis 24

您好,我希望定期从 table 中抓取值,例如这个 https://www.tennis24.com/match/Wra2Ija2/#match-statistics;0。在一个理想的世界中,我想在页面发生变化时抓取。 (我不知道这是否可行。)

我想每 3 分钟检查一次。这是一个好主意吗?或者有更简单的方法吗?

此外,这是我的代码:
虽然它只拉玩家 Bs A...

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Chrom_path = r"C:\Users\Dan1\Desktop\chromedriver.exe"
driver = webdriver.Chrome(Chrom_path)
driver.get("https://www.tennis24.com/match/hOYDXnLI/#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"))

table 中的数据具有带有标签 <div class="statTextGroup">

的迭代模式

其中包含3个子节点:

<div class="statText statText--homeValue">0</div>

<div class="statText statText--titleValue">Aces</div>

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

分别为主场球员数据,数据标签,客场球员数据。

我的以下脚本遍历这些节点并打印内部文本内容:

from selenium import webdriver

driver = webdriver.Chrome("../chromedriver")
driver.get("https://www.tennis24.com/match/hOYDXnLI/#match-statistics;0")

data = driver.find_elements_by_class_name("statTextGroup")
for d in data:
    sub_data = d.find_elements_by_xpath(".//*")
    assert len(sub_data)==3
    for s_d in sub_data:
        print(s_d.get_attribute('class')[19:], s_d.get_attribute('innerText'))

driver.close()

显示的输出如下:

homeValue 0
titleValue Aces
awayValue 3
homeValue 1
titleValue Double Faults
awayValue 0
homeValue 58%
titleValue 1st Serve Percentage
awayValue 62%
homeValue 60% (9/15)
titleValue 1st Serve Points Won
awayValue 45% (15/33)
homeValue 73% (8/11)
titleValue 2nd Serve Points Won

请注意,这些数据模式对于整个游戏数据重复 4 次(以您的示例为例),第 1 组、第 2 组和第 3 组

注意标记为“Aces”的数据重复出现