【问题标题】:Scraping same class Selenium刮取同级Selenium
【发布时间】:2021-05-19 13:07:32
【问题描述】:

我想从这个页面上刮掉主队和客队https://www.flashscore.com/match/hY5c1Bhh/#match-summary/match-summary

    # Get HomeTeam
    _ht = driver.find_element_by_xpath('//*[contains(@class, "home")]')
    ht = _ht.find_element_by_xpath('//*[contains(@class, "participantName")]')
    _homeName = ht.text
    
    # Get AwayTeam
    _at = driver.find_element_by_xpath('//*[contains(@class, "away")]')
    at = _at.find_element_by_xpath('//*[contains(@class, "participantName")]')
    _awayName = at.text

输出

Longford
Longford

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    尝试将它们都存储在这样的列表中:

    teams = driver.find_elements(By.CSS_SELECTOR, "div[class^='participantName'] a")
    print("Home team : ", teams[0].text)
    print("Away team : ", teams[1].text)
    

    【讨论】:

    • 我在此页面上尝试了相同的分数flashscore.com/match/OxfWpyjH/#match-summary/match-summaryscores = driver.find_elements(By.CSS_SELECTOR, "div[class^='incidentsHeader']") _1hf = scores[0].text _2hf = scores[1].text,但得到了IndexError: list index out of range
    • @luca 使用相同的 CSS_SELECTOR 如果您要提取团队名称 div[class^='participantName'] a 相同的代码应该可以工作。
    • @cruisepandey 是的,它有效,我可以提取团队名称。但我也必须在页面中间刮分数。我尝试将 CSS_SELECTOR 用于"div[class^='incidentsHeader']")
    • @luca : 试试这个css选择器div[class^='matchInfo'] div[class^='wrapper']
    • @cruisepandey 不起作用空白输出先前的代码索引超出范围
    【解决方案2】:

    您在尝试在其他元素中定位元素时缺少.
    所以你的代码应该是

    # Get HomeTeam
        _ht = driver.find_element_by_xpath('//*[contains(@class, "home")]')
        ht = _ht.find_element_by_xpath('.//*[contains(@class, "participantName")]')
        _homeName = ht.text
        
        # Get AwayTeam
        _at = driver.find_element_by_xpath('//*[contains(@class, "away")]')
        at = _at.find_element_by_xpath('.//*[contains(@class, "participantName")]')
        _awayName = at.text
    

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 2022-01-02
      • 2020-03-21
      • 1970-01-01
      • 2021-12-24
      相关资源
      最近更新 更多