Reputation: 113
I'm using Selenium to extract data from a website. The HTML is usually the same for all the shows but for certain shows it's different. Most should have 27 elements but some have lower amounts so it spits out an error
So, I'm asking if there is a way to break out of the section of the code before the error stops the program?
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)
This is my code The website I'm using is MyAnimeList
Upvotes: 1
Views: 476
Reputation: 57
This answers goes for any code in Python that causes an Exception.
To catch an exception so that the program continues to run, use the try
and except
block. Within the try
block put the code that may cause an exception. If an exception does trigger then the except
block will run.
For example:
try:
my_int = int(input("Enter a number: "))
except ValueError:
print("Please enter a number not a text")
In the above example the user may enter an integer in to the command line and the code will run smoothly, but if a user instead enters a character or a string such as hello world
the int()
function will raise a ValueError
which will be caught by the except
block which will print an error message but won`t stop the program.
If multiple exceptions may occur you can use multiple except
block to catch different kinds of exceptions. You can also use an empty except
block that will catch any exception (though this is generally unadvisable).
If you want to learn more about try ecxpet
blocks and exception handling in general in python see here.
Upvotes: 1
Reputation: 29362
Yes it is use try, except :
Let's say you think that the few line of code will have some issue (risky code), try to put that into try clause, like below :
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
Even if it fails in a for loop, it will go to except that just says pass.
Upvotes: 1