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。在
- 项目
标签: