python库

fastlinkcheck的Python项目详细描述


快速链接检查

Check for broken external and internal links.

fastlinkcheck检查HTML文档中的断开链接。这是并行的,所以性能很快。检查外部链接和内部链接。通过验证本地文件检查内部链接。在

要开始,请阅读the documentation。在

安装

pip install fastlinkcheck

使用

安装fastlinkcheck后,可以从命令行使用cli命令link_check。我们可以看到带有--help标志的各种选项。在

link_check --help
^{pr2}$

我们可以递归地在这个repo中的目录_example/broken_links中搜索断开的链接,如下所示:

link_check _example/broken_links 
ERROR: The Following Broken Links or Paths were found:

- 'http://fastlinkcheck.com/test.html' was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

- 'http://somecdn.com/doesntexist.html' was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

- Path('/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.js') was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

通过指定--host参数,可以通过标识具有该主机名的链接来检测内部链接。外部链接通过向适当的网站提出请求进行验证。另一方面,通过检查本地文件的存在和内容来验证内部链接。在

当使用--host参数只传递主机(和路径,如果需要)而不使用协议时,我们必须小心。例如,如果站点的url是http://fastlinkcheck.com/test.html,那么我们是如何指定主机名的:

link_check _example/broken_links --host fastlinkcheck.com
ERROR: The Following Broken Links or Paths were found:

- 'http://somecdn.com/doesntexist.html' was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

- Path('/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.js') was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

我们现在少了一个断开的链接,因为在我们搜索的路径根中确实有一个名为test.html的文件。但是,如果我们在--host的末尾添加一个路径,例如fastlinkcheck.com/mysite,则链接将再次被列为断开,因为_example/broken_links/mysite/test.html不存在:

link_check _example/broken_links --host fastlinkcheck.com/mysite
ERROR: The Following Broken Links or Paths were found:

- 'http://fastlinkcheck.com/test.html' was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

- 'http://somecdn.com/doesntexist.html' was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

- Path('/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.js') was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

可以通过创建包含要忽略的URL和路径列表的文本文件来忽略链接。例如,文件_example/broken_links/linkcheck.rc包含:

cat _example/broken_links/linkcheck.rc
test.js
https://www.google.com

我们可以使用这个文件来忽略带有--config_file参数的url和路径。这将从我们先前的结果中筛选出对断开链接/test.js的引用:

link_check _example/broken_links --host fastlinkcheck.com --config_file _example/broken_links/linkcheck.rc
ERROR: The Following Broken Links or Paths were found:

- 'http://somecdn.com/doesntexist.html' was found in the following pages:
  - `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`

最后,如果没有断开的链接,link_check将不返回任何内容。目录_example/no_broken_links/不包含任何链接断开的HTML文件:

link_check _example/no_broken_links
No broken links found!

Python

您也可以从python而不是终端使用这些实用程序。有关详细信息,请参见these docs。在

在GitHub操作中使用link_check

以下是如何在GitHub操作中使用此实用程序的示例:

name:Check Linkson:[workflow_dispatch,push]jobs:check-links:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v2-uses:actions/setup-python@v2-name:check for broken linksrun:|pip install fastlinkchecklink_check _example 

当发现断开的链接时,我们可以多写几行代码来打开问题,使用gh cli

...-name:check for broken linksrun:|pip install fastlinkchecklink_check _example 2> err || trueexport GITHUB_TOKEN="YOUR_TOKEN"[[ -s err ]] &&  gh issue create -t "Broken links found" -b "$(< err)" -R "yourusername/yourrepo"

我们可以进一步扩展此功能,仅在具有特定标签的另一个问题尚未打开时打开问题:

...-name:check for broken linksrun:|pip install fastlinkchecklink_check "docs/_site" --host "docs.fast.ai" 2> err || trueexport GITHUB_TOKEN="YOUR_TOKEN"if [[ -z $(gh issue list -l "broken-link")) && (-s err) ]]; thengh issue create -t "Broken links found" -b "$(< err)" -l "broken-link" -R "yourusername/yourrepo"fi

有关详细信息,请参阅GitHub Actions docs。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
来自偏移量的java JavaPairInputStream流   gzip Java TarInputStream读取tar的文件名。包含另一个tar的gz文件。gz文件   如何在Linux shell脚本中确定和使用实际的java路径   java请求太多FirebaseError   java错误json解析   java在localserver上发送安卓应用程序的输出   JavaSpring:启动和停止webapplication时更新DBtable条目   java如何使用JavaFX在矩形处设置文本?   java SQL查询在Hibernate中出现异常   java我无法使用javamail代码通过outlook(hotmail帐户)配置发送邮件,但在gmail中工作正常   java是不同的持久性。测试运行/JPA装置的xml属性   无头Eclipse的java导出战   使用ContentVersionStrategy的java Spring引导缓存禁止使用gzip进行资源压缩   java如何获取计算机的设备令牌?   图像文件的java ImageInfo不包含有效值   继承强制在Java中使用基类方法