【问题标题】:Get text from a table with selenium python使用 selenium python 从表中获取文本
【发布时间】: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


【解决方案1】:

如前文所述,有 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']

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      这将为您提供所需的 2 张桌子。通过find_element_by_tag_name("tr") 遍历集合以获取相应的表行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 2018-07-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多