Selenium Cicle中的超时异常

问题描述

我正在抓取此页面https://www.betexplorer.com/soccer/netherlands/eerste-divisie-2018-2019/results/,我的代码提取了所有匹配的网址,并用提示for提取了每个网址的数据。 (网址示例https://www.betexplorer.com/soccer/netherlands/eerste-divisie-2018-2019/den-bosch-g-a-eagles/YkOxU6sM/),这是我的代码

for i in matches:
    driver.get(i)

    Country = webdriverwait(driver,2).until(EC.visibility_of_element_located((By.XPATH,"/html/body/div[4]/div[5]/div/div/div[1]/section/ul/li[3]/a"))).text
    leagueseason = webdriverwait(driver,"/html/body/div[4]/div[5]/div/div/div[1]/section/header/h1/a"))).text
    Date = webdriverwait(driver,2).until(EC.visibility_of_element_located((By.ID,'match-date'))).text
    Date = Date.replace(".","/").replace("-","").replace(" ","",1)
    Home = webdriverwait(driver,"/html/body/div[4]/div[5]/div/div/div[1]/section/ul[2]/li[1]/h2/a"))).text
    Away = webdriverwait(driver,"/html/body/div[4]/div[5]/div/div/div[1]/section/ul[2]/li[3]/h2/a"))).text
    ft = webdriverwait(driver,'js-score'))).text
    Res = ""
    
    try:
        extrainfo = driver.find_element_by_xpath("//*[@id='js-eventstage']").text
    except NoSuchElementException:
        extrainfo = " "

    try:
        driver.find_element_by_xpath("//td[a[.='bet365']]/following-sibling::td[span]")
        webdriverwait(driver,2).until(EC.element_to_be_clickable((By.XPATH,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).click()
        oid = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).get_attribute("data-oid")
        bid = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).get_attribute("data-bid")
        var = oid+'-'+bid
    except NoSuchElementException:
        B365H = 'no bet365 odd'
    except TypeError:
        B365H = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[1]"))).get_attribute("data-odd")
    else:
        B365H = webdriverwait(driver,"//*[contains(@id,'%s')]/tr[last()]/td[@class='bold']" % var))).text

    
    try:
        driver.find_element_by_xpath("//td[a[.='bet365']]/following-sibling::td[span]")
        webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[2]"))).click()
        oid = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[2]"))).get_attribute("data-oid")
        bid = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[2]"))).get_attribute("data-bid")
        var = oid+'-'+bid
    except NoSuchElementException:
        B365D = 'no bet365 odd'
    except TypeError:
        B365D = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[2]"))).get_attribute("data-odd")
    else:
        B365D = webdriverwait(driver,'%s')]/tr[last()]/td[@class='bold']" % var))).text


    
    try:
        driver.find_element_by_xpath("//td[a[.='bet365']]/following-sibling::td[span]")
        webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[3]"))).click()
        oid = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[3]"))).get_attribute("data-oid")
        bid = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[3]"))).get_attribute("data-bid")
        var = oid+'-'+bid
    except NoSuchElementException:
        B365A = 'no bet365 odd'
    except TypeError:
        B365A = webdriverwait(driver,"(//td[a[.='bet365']]/following-sibling::td[@data-odd])[3]"))).get_attribute("data-odd")
    else:
        B365A = webdriverwait(driver,'%s')]/tr[last()]/td[@class='bold']" % var))).text

现在,当页面未加载时,我将添加一个TimeoutException,并以此方式进行尝试:

try:
   #prevIoUs code
except TimeoutException:
   driver.get(i)

这样,当我遇到超时错误时,代码将跳过抓取网址并继续下一个网址。 我该如何解决这个问题?我希望在发生超时的情况下,它将重新加载页面并再次为该URL抓取数据。

解决方法

您可以将其放入while循环中,一旦成功完成,它将继续进行。像这样:

success = False
while success == False:
    try:
        # previous code
        success = True
    except TimeoutException:
        print ('Will try again...')