AttributeError
The AttributeError in BeautifulSoup is raised when an invalid attribute reference is made, or when an attribute assignment fails. When during the execution of code we pass the wrong attribute to a function that attribute doesn’t have a relation with that function then AttributeError occurs. When we try to access the Tag using BeautifulSoup from a website and that tag is not present on that website then BeautifulSoup always gives an AttributeError.
We take a good example to explain the concept of AttributeError with web scraping using BeautifulSoup:
Python3
# importing modules import requests import bs4 # getting response from server response = requests.get(url) # extracting html soup = bs4.BeautifulSoup(response.text, 'html.parser' ) # for printing attribute error print (soup.NoneExistingTag.SomeTag) |
Output:
XML Parser Error :
We all are gone through XML parser error during coding the web scraping scripts, by the help of BeautifulSoup we parse the document into HTML very easily. If we stuck on the parser error then we easily overcome this error by using BeautifulSoup, and it is very easy to use.
When we’re parsing the HTML content from the website we generally use ‘ xml ‘ or ‘ xml-xml ‘ in the parameter of BeautifulSoup constructor. It was written as the second parameter after the HTML document.
Syntax:
soup = bs4.BeautifulSoup( response, ‘ xml ‘ )
or
soup = bs4.BeautifulSoup( response, ‘ xml -xml’ )
XML parser error generally happens when we’re not passing any element in the find() and find_all() function or element is missing from the document. It sometimes gives the empty bracket [] or None as their output.
Python
import requests import bs4 response = requests.get(url) soup = bs4.BeautifulSoup(response.text, 'xml' ) print (soup.find( 'div' , class_ = 'that not present in html content' )) |
Output:
BeautifulSoup – Error Handling
Sometimes, during scraping data from websites we all have faced several types of errors in which some are out of understanding and some are basic syntactical errors. Here we will discuss on types of exceptions that are faced during coding the script.