Last active
September 16, 2021 14:25
-
-
Save Peterragheb/e47e687f95b0e7cf6a79e96b281fb071 to your computer and use it in GitHub Desktop.
Simple Website Broken Links Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
from bs4 import BeautifulSoup | |
import validators | |
def extract_all_links(site): | |
html = requests.get(site).text | |
soup = BeautifulSoup(html, 'html.parser').find_all('a') | |
links = [link.get('href') for link in soup] | |
unique_links = set(links) | |
return unique_links | |
site_link = input('Enter URL of the site : ').rstrip("/") | |
links = extract_all_links(site_link) | |
for link in links: | |
if link.startswith("https://") and site_link not in link: | |
continue | |
if not link.startswith("https://"): | |
link = site_link + link | |
if not validators.url(link): | |
continue | |
r = requests.get(url = link) | |
time.sleep(1) | |
if r.status_code != 200: | |
print(str(r.status_code) + " " + link) | |
else: | |
print("Success "+ link) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment