使用 selenium python 从 table 获取文本

Get text from a table with selenium python

使用此代码,我试图从 webpage

中名为“最后一场比赛”的 table 中提取文本
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 场比赛)

感谢大家的帮助,感谢大家

使用 find_elemnts_by_xpath 使用 xpath //strong[text()='Last matches']/ancestor::div[6]//following-sibling::tbody

这将为您提供所需的 2 table。按 find_element_by_tag_name("tr") 遍历集合以获取相应的 table 行

您可以使用以下xpath //table[@class='table']css_selector table[class='table']
这将为您提供您正在寻找的 2 个表格。
在它们里面你可以清楚地得到分数并将它们放入单独的列表中。

如前面post所述,有2个表,因此您需要分别对待它们以获得您想要的列表

last_matches_team1 = []
last_matches_team2 = []

left_table = "(//table[@class='table'])[1]//tr[@class='cursor-pointer']//td[6]//div[@aria-label='Score']"
for row in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, (left_table)))):
    score = row.get_attribute("innerText")
    last_matches_team1.append(score)
print(last_matches_team1)

right_table = "(//table[@class='table'])[2]//tr[@class='cursor-pointer']//td[6]//div[@aria-label='Score']"
for row in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, (right_table)))):
    score = row.get_attribute("innerText")
    last_matches_team2.append(score)
print(last_matches_team2)

打印:

['2:0', '0:4', '3:4', '2:2', '0:1', '3:0']
['2:0', '0:4', '1:0', '2:1', '1:1', '1:2']