当程序在Selenium Python中遇到错误时,有没有办法让它保持运行

2024-05-17 13:51:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium从网站中提取数据。所有节目的HTML通常是相同的,但某些节目的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:\Users\amete\Documents\chromedriver.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


2条回答

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

例如:

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

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

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

如果您想了解有关python中try ecxpet块和异常处理的更多信息,请参阅here

是,请尝试使用,但以下情况除外:

假设您认为这几行代码会有一些问题(风险代码),请尝试将其放入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循环中失败,它也会转到,只是说pass

相关问题 更多 >