有没有办法让程序在Selenium Python中遇到错误时继续运行



我正在使用Selenium从网站中提取数据。所有节目的HTML通常是相同的,但对于某些节目,它是不同的。大多数应该有 27 个元素,但有些元素的数量较少,所以它会吐出一个错误

所以,我问是否有办法在错误停止程序之前突破代码部分?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
Anime = input("Enter Anime:")
driver = webdriver.Chrome(executable_path=r"C:UsersameteDocumentschromedriver.exe")
driver.get("https://myanimelist.net/search/all?q=one%20piece&cat=all")
print(driver.title)
search = driver.find_element_by_xpath('//input[@name="q"]')
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="q"]')))
# Clears the field
search.send_keys(Keys.CONTROL, 'a')
search.send_keys(Keys.DELETE)
# The field is now cleared and the program can type whatever it wants
search.send_keys(Anime)
search.send_keys(Keys.RETURN)
#Accept the cookies
accept = driver.find_element_by_xpath('//*[@id="qc-cmp2-ui"]/div[2]/div/button[3]').click()
#  Added this wait
wait.until(EC.element_to_be_clickable((By.XPATH, '//h2[@id="anime"]//ancestor::div[@class="content-left"]//article[1]/div[contains(@class, "list")][1]/div[contains(@class, "information")]/a[1]')))
link = driver.find_element_by_xpath('//h2[@id="anime"]//ancestor::div[@class="content-left"]//article[1]/div[contains(@class, "list")][1]/div[contains(@class, "information")]/a[1]').click()
#Extracting information
#English Title + Jap Title + Display Score
English_Title = driver.find_element_by_xpath('//*[@id="contentWrapper"]/div[1]').text
#Jap_Title = driver.find_element_by_xpath('//*[@id="contentWrapper"]/div[1]/div/div[1]/div/h1/strong').text
Score = driver.find_element_by_xpath('//*[@id="content"]/table/tbody/tr/td[2]/div[1]/table/tbody/tr[1]/td/div[1]/div[1]/div[1]/div[1]/div[1]/div').text
Episodes = driver.find_element_by_xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[10]').text
print ("The English title :"+English_Title)
#print ("The Japanese title:"+Jap_Title)
print ("The Score of the Anime is:"+str(Score))
print (Episodes)
for i in range (7,28):
Info = driver.find_element_by_xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div['+str(i)+']').text
print (Info)

这是我的代码 我正在使用的网站是MyAnimeList

是的,它是使用 try,除了:

假设您认为几行代码会出现一些问题(有风险的代码),请尝试将其放入try子句中,如下所示:

try:
for i in range (7,28):
Info = driver.find_element_by_xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div['+str(i)+']').text
print (Info)
except:
pass

即使它在 for 循环中失败,它也会转到,除了只是说通过。

这个答案适用于 Python 中导致异常的任何代码。 若要捕获异常以便程序继续运行,请使用tryexcept块。在try块中放置可能导致异常的代码。如果确实触发了异常,则except块将运行。

例如:

try:
my_int = int(input("Enter a number: "))
except ValueError:
print("Please enter a number not a text")

在上面的示例中,用户可以在命令行中输入一个整数,代码将顺利运行,但如果用户改为输入字符或字符串(如hello world),int()函数将引发一个ValueError,该将被except块捕获,该块将打印错误消息,但不会停止程序。

如果可能发生多个异常,您可以使用多个except块来捕获不同类型的异常。您还可以使用空的except块来捕获任何异常(尽管这通常是不可取的)。

如果你想了解更多关于 python 中try ecxpet块和异常处理的信息,请参阅此处。

最新更新