您好,我希望定期从诸如此类的表中https://www.tennis24.com/match/Wra2Ija2/#match-statistics;0抓取这些值。在理想的情况下,页面要进行更改时,我要抓取。 (我什至不知道这是否有可能做。)
我想每3分钟检查一次。这是一个好主意吗?还是有更简单的方法?
另外,这是我的代码:
它只会吸引玩家Bs Ace ...
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"))
答案 0 :(得分:1)
表中的数据具有带有标签<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”的数据时要注意