【发布时间】:2021-05-11 14:34:27
【问题描述】:
使用此代码,我试图从 webpage 中名为“最后匹配项”的表中提取文本
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = 'https://s5.sir.sportradar.com/sports4africa/en/1/season/80526/headtohead/334075/340986/match/27195664'
driver = webdriver.Edge("C:/Users/Hama/Documents/msedgedriver.exe")
driver.get(url)
driver.implicitly_wait(10)
WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH, "//strong[text()='Last matches']/ancestor::div[6]//tbody/tr")))
rows= driver.find_elements_by_xpath("//strong[text()='Last matches']/ancestor::div[6]//tbody/tr")
All_last_matches = []
for res in rows:
score = res.find_element_by_xpath(".//td[5]//div[@class=' no-wrap']").get_attribute("innerText")
All_last_matches.append(score)
print(All_last_matches)
它给了我这个列表:
All_last_matches = ['2:0', '0:4', '3:4', '2:2', '0:1', '3:0', '2:0', '0:4', '1:0', '2:1', '1:1', '1:2']
如何修改我的代码以获得两个这样的列表:
Last_matches_team1 = ['2:0', '0:4', '3:4', '2:2', '0:1', '3:0']
Last_matches_team2 = ['2:0', '0:4', '1:0', '2:1', '1:1', '1:2']
我试过了:
Last_matches_team1 = All_last_matches[0:6]
Last_matches_team2 = All_last_matches[6:len(All_last_matches)]
但这只有在 table1 有 6 行时才有效,有时只有 5 行(播放 5 场比赛)
感谢大家的帮助
【问题讨论】:
-
你为什么不先检查一下len?
-
即使我对列表中的 len 表示不满也无济于事,因为我不知道结果属于 team2 的哪个索引
-
有没有办法从页面结构/HTML中区分分数?
-
提供的代码不是我的,感谢@vitaliis帮助我创建它,我是新手,所以我不知道如何解决这个问题
标签: python selenium selenium-webdriver xpath webdriver