這是我的代碼: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, 2).until(EC.visibility_of_element_located((By.XPATH, "/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, 2).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[4]/div[5]/div/div/div[1]/section/ul[2]/li[1]/h2/a"))).text? ? Away = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[4]/div[5]/div/div/div[1]/section/ul[2]/li[3]/h2/a"))).text? ? ft = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.ID, 'js-score'))).text? ? Res = ""? ??? ? try:? ? ? ? extrainfo = driver.find_element_by_xpath("http://*[@id='js-eventstage']").text? ? except NoSuchElementException:? ? ? ? extrainfo = " "現(xiàn)在我會TimeoutException在頁面未加載時添加一個,我嘗試這樣做:try:? ?#previous codeexcept TimeoutException:? ?driver.get(i)這樣,當我遇到超時錯誤時,代碼將跳過 url 抓取并繼續(xù)處理下一個 url。我該如何解決這個問題?我希望在超時的情況下它會重新加載頁面并再次抓取該網(wǎng)址的數(shù)據(jù)。
1 回答

大話西游666
TA貢獻1817條經(jīng)驗 獲得超14個贊
您可以將其放入 while 循環(huán)中,一旦成功完成,就會繼續(xù)。所以像這樣:
success = False
while success == False:
try:
# previous code
success = True
except TimeoutException:
print ('Will try again...')
添加回答
舉報
0/150
提交
取消