Error During Fetching of Website
When we are fetching any website content we need to aware of some of the errors that occur during fetching. These errors may be HTTPError, URLError, AttributeError, or XMLParserError. Now we will discuss each error one by one.
HTTPError:
HTTPError occurs when we’re performing web scraping operations on a website that is not present or not available on the server. When we provide the wrong link during requesting to the server then and we execute the program is always shows an Error “Page Not Found” on the terminal.
Example :
Python3
# importing modules import requests from urllib.error import HTTPError try : response = requests.get(url) response.raise_for_status() except HTTPError as hp: print (hp) else : print ( "it's worked" ) |
Output:
The link we provide to the URL is running correctly there is no Error occurs. Now we see HTTPError by changing the link.
Python
# importing modules import requests from urllib.error import HTTPError try : response = requests.get(url) response.raise_for_status() except HTTPError as hp: print (hp) else : print ( "it's worked" ) |
Output:
URLError:
When we request the wrong website from the server it means that URL which we are given for requesting is wrong then URLError will occur. URLError always responds as a server not found an error.
Example:
Python3
# importing modules import requests from urllib.error import URLError try : response = requests.get(url) response.raise_for_status() except URLError as ue: print ( "The Server Could Not be Found" ) else : print ( "No Error" ) |
Output:
Here we see that the program executes correct and print output “No Error”. Now we change the URL link for showing the URLError :-
Python3
# importing modules import requests from urllib.error import URLError try : response = requests.get(url) response.raise_for_status() except URLError as ue: print ( "The Server Could Not be Found" ) else : print ( "No Error" ) |
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.