Package: pyanchor
PyAnchorDead links are an annoyance for websites with an extensive amount of content. A side from the
negative impact on SEO, dead links are an annoyance for any user that clicks on one.PyAnchor is primarily for checking the HTTP response on all links on a page. You can integrate it
into your development workflow so that users never see a 404 in the first place.InstallPyAnchor requires Python 3.7 and above.MacOS / Linux:$python3-mpipinstallpyanchorWindows:> python -m pip install pyanchorUsing the CLIThe CLI can be invoked with thepyanchorcommand. A URLmustbe provided unless it's the help page.To get the help page:>pyanchor--helpBasic example for a single page:Note: all provided URLs must include a valid HTTP scheme.>pyanchorhttps://mysite.com/If you want to check all links on a website, and not just a single page, asitemap.xmlURL may be
provided and flagged with--sitemap.Example:>pyanchorhttps://mysite.com/sitemap.xml--sitemapBy default, successful requests are not printed to the terminal. To see all urls with a200response add the--verboseflag.>pyanchorhttps://mysite.com--verbose>pyanchorhttps://mysite.com/sitemap.xml--sitemap--verboseBut wait, there's more...To integrate PyAnchor into your application, you can import theLinkResultsclass.LinkResultsrequires a URL.Example:>>> from pyanchor.link_checker import LinkResults
>>> r = LinkResults("https://mysite.com/")
>>> r.results
{200: ["https://mysite.com/about/", "https://mysite.com/contact/"], 500: ["https://mysite.com/doh!/"]}As you can see theresultsattribute is a dictionary containing all response codes returned as a
dictionary key, with a list of URLs that achieve that response code as the dictionary value.Analyzing LinksPyAnchor give you the ability to use theLinkAnalysisclass to check the links in a given URL for unsafe and obsolete attributes.To check for obsolete attributes use theobsolete_attrsproperty:>>> from pyanchor.link_checker import LinkAnalysis
>>> r = LinkAnalysis("https://mysite.com/")
>>> r.obsolete_attrs
{'/about/link-1': ['charset', 'rev'], '/about/link-2': ['name']}Likewise you can check for unsafe linkes withunsafe_attrs:>>> from pyanchor.link_checker import LinkAnalysis
>>> r = LinkAnalysis("https://mysite.com/")
>>> r.unsafe_attrs
{<a href="/about/link-4" target="_blank">Link 4</a>: True, <a href="/about/link-5" rel="noreferrer noopener" target="_blank">Link 5</a>: False}Any link thatdoes notincluderel="noopener"when thetargetattribute is used will returnTrue. As in,it is True that this link is unsafe. Therfore, links with appropriate attributes will returnFalse.FeedbackIf you find a bug, pleasefile an issue.If you have feature requests, pleasefile an issueand use the appropriate label.SupportIf you would like to show your support for the project, I would be very grateful if you would donate
to a charity close to my heart,Walk AS One.And if you would prefer to donate to me personally instead,you can sponsor me on Github? 🤓How to ContributePleaseraise an issuebefore making a PR, so that the issue and implementation can be discussed before you write any code.This will save you time, and increase the chances of your PR being merged without significant changes.Please make PR's on anew branch, andnoton main/master.Pleaseformat you codewithBlack.Pleaseinclude testsfor any PR's that include code (unless current tests cover your code contribution).ContributorsThank you to:mrbazzanfor PR#25Icelainfor PR#11wevnascfor PR#8muditshamzfor PR#6