Last active
August 1, 2024 12:47
-
-
Save kayvank/0d43254416b15dc9c493140c19ccc2a4 to your computer and use it in GitHub Desktop.
find-broken-http-links
This file contains 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
#!/usr/bin/env python3 | |
# Broken Links on www Site | |
import sys | |
from bs4 import BeautifulSoup | |
from urllib.parse import urlparse | |
from urllib.parse import urljoin | |
import requests | |
searched_links = [] | |
broken_links = [] | |
def getLinksFromHTML(html): | |
def getLink(el): | |
return el["href"] | |
return list(map(getLink, BeautifulSoup(html, features="html.parser").select("a[href]"))) | |
def find_broken_links(domainToSearch, URL, parentURL): | |
if (not (URL in searched_links)) and (not URL.startswith("mailto:")) and (not ("javascript:" in URL)) and (not URL.endswith(".png")) and (not URL.endswith(".jpg")) and (not URL.endswith(".jpeg")): | |
try: | |
requestObj = requests.get(URL); | |
searched_links.append(URL) | |
if(requestObj.status_code == 404): | |
broken_links.append("BROKEN: link " + URL + " from " + parentURL) | |
print(broken_links[-1]) | |
else: | |
print("NOT BROKEN: link " + URL + " from " + parentURL) | |
if urlparse(URL).netloc == domainToSearch: | |
for link in getLinksFromHTML(requestObj.text): | |
find_broken_links(domainToSearch, urljoin(URL, link), URL) | |
except Exception as e: | |
print("ERROR: " + str(e)); | |
searched_links.append(domainToSearch) | |
find_broken_links(urlparse(sys.argv[1]).netloc, sys.argv[1], "") | |
print("\n--- DONE! ---\n") | |
print("The following links were broken:") | |
for link in broken_links: | |
print ("\t" + link) |
This file contains 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
{ pkgs ? import <nixpkgs> {} }: | |
let | |
my-python-packages = ps: with ps; [ | |
requests | |
beautifulsoup4 | |
uritools | |
pip | |
# other python packages | |
]; | |
my-python = pkgs.python3.withPackages my-python-packages; | |
in pkgs.mkShell { | |
buildInputs = [ | |
my-python | |
pkgs.cowsay | |
]; | |
shellHook = | |
'' | |
echo 'to find your broken links: ./brokenlinks.py "http://my-site.html" ' | cowsay | |
''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
$ nix-shell
$ brokenlinks.py https://docs.cardano.org/introduction > ./report.txt 2>&1