从网球上刮下一张桌子

2024-06-25 23:57:58 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我希望定期从这样一个表https://www.tennis24.com/match/Wra2Ija2/#match-statistics;0中刮取值。在一个理想的世界里,当页面发生了变化时,我想去刮擦(我甚至不知道这是否可行。)

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

还有,这是我的代码:
不过,这只会吸引玩家B 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"))

Tags: fromhttpsimportcomsupportbywwwmatch
1条回答
网友
1楼 · 发布于 2024-06-25 23:57:58

表中的数据有一个带有标记<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

请注意,对于整场比赛数据,第1组、第2组和第3组,这些数据模式重复4次(例如)

重复标记为“Aces”的数据时请注意

相关问题 更多 >