Last modified: Jan 10, 2023 By Alexander Williams
How to Build a Broken Link Checker Tool in Python
In this article, we are going to write a simple checker link tool.
Technology:
- requests
Before starting, we need to install the requests library.
Installation via pip:
pip install requests
Table Of Contents
Checker link tool
import requests
def link_checker(link):
try:
#GET request
req = requests.get(link)
#check status-code
if req.status_code in [400,404,403,408,409,501,502,503]:
return(f"{link} => Broken status-code:{req.status_code}")
else:
return(f"{link} => Good")
#Exception
except requests.exceptions.RequestException as e:
# print link with Errs
raise SystemExit(f"{link}: Somthing wrong \nErr: {e}")
Let me explain step by step.
- Send a get request for URL
- Check the status code of the request
- If the status code is in the list of all HTTP error codes, return...
- If not, return...
Usage:
link_checker("https://pytutorial.com/check-url-is-reachable")
Output:
https://pytutorial.com/check-url-is-reachable => Good
link_checker("https://pytutorial.com/notfound")
Output:
https://pytutorial.com/notfound => Broken status-code:404
Check multiple links:
links = (
'https://pytutorial.com/check-if-a-list-has-an-item-python',
'https://pytutorial.com/python-variable-in-string',
'https://pytutorial.com/requests-get-status-code',
'https://pytutorial.com/notfound,',
)
for link in links:
c = link_checker(link)
print(c)
Output:
https://pytutorial.com/check-if-a-list-has-an-item-python => Good https://pytutorial.com/python-variable-in-string => Good https://pytutorial.com/requests-get-status-code => Good https://pytutorial.com/notfound, => Broken status-code:404
Finally, this is just a simple tool to understand how it works.
I hope this article helps you.