1

我正在制作一个抓取器来抓取一个博彩网站的数据,这是一个示例代码,它将抓取事件、团队和赔率。这个想法是迭代所有可用于这项运动的比赛。我能够找到链接并单击第一个链接,然后我还能够抓取数据并将其返回到列表中(稍后将被放入数据库中)。 我的问题是我无法返回上一页单击下一个链接,尝试执行此操作时出现错误。这是我的代码:

driver = webdriver.Chrome(Path)
driver.get("https://www.neds.com.au/sports/table-tennis/")
a = []

links = driver.find_elements_by_class_name("matches-filter__link")

for l in links:
    
    l.click()
    tt_matches = driver.find_elements_by_class_name("sport-event-card")
    for match in tt_matches:
        Match = match.find_element_by_css_selector(".sports-event-title__name-text").text
        a.append(Match)
        Teams = match.find_elements_by_css_selector(".price-button")
        for team in Teams:
            team_name = team.find_element_by_css_selector(".price-button-name").text
            team_odd = team.find_element_by_css_selector(".price-button-odds-price span").text
            a.append(team_name)
            a.append(team_odd)
    driver.back()
    time.sleep(2)

driver.quit()

它返回此错误:

引发异常类(消息、屏幕、堆栈跟踪)

StaleElementReferenceException:陈旧的元素引用:元素未附加到页面文档(会话信息:chrome = 84.0.4147.105)

我该如何解决这个问题,我认为问题出在 driver.back() 上,我也尝试了 l.back(),但它仍然无法正常工作。

4

1 回答 1

2

在这种情况下,您需要收集 URL,然后导航到它们。您可以使用以下代码:

driver.get("https://www.neds.com.au/sports/table-tennis/")
time.sleep(5)
a = []

links = driver.find_elements_by_class_name("matches-filter__link")
urls = [l.get_attribute('href') for l in links]
for u in urls:
    print(u)
    driver.get(u)
    tt_matches = driver.find_elements_by_class_name("sport-event-card")
    for match in tt_matches:
        Match = match.find_element_by_css_selector(".sports-event-title__name-text").text
        a.append(Match)
        Teams = match.find_elements_by_css_selector(".price-button")
        for team in Teams:
            team_name = team.find_element_by_css_selector(".price-button-name").text
            team_odd = team.find_element_by_css_selector(".price-button-odds-price span").text
            a.append(team_name)
            a.append(team_odd)
    time.sleep(2)

print(a)
driver.quit()
于 2020-08-05T07:16:58.787 回答