html检查链接正确代码,Groovy脚本检查html坏链接
這些天在搞Gradle翻譯,因為原譯者在翻譯的同時也把文件進行了整理,并且把翻譯過的章節放到新的文件夾中,導致可能有些超鏈接未改正過來變成死鏈接。
本想在網上找個工具來檢查的,百度了幾個工具要么太大要么要安裝,懶得弄那么多,于是用Groovy寫了一個腳本。此腳本僅檢查本地超鏈接,代碼如下:
if (args.size() != 1) {
printf("Please specify a folder or HTML file path...")
return
}
def file = new File(args[0])
if(file.isFile()) {
if(!args[0].toLowerCase().endsWith(".html")) {
return
}
checkHtml(file)
} else if (file.isDirectory()) {
def errorLinks = new HashMap>()
file.eachFileMatch( ~/.*\.html/, {
checkHtml(it, errorLinks)
})
errorLinks.each {name, links ->
println "file: " + name
links.each {
println "href:\t" + it
}
}
}
void checkHtml(File file, HashMap> errorlinks) {
def matches = file.text.findAll('href="([^#(http)].+?)("|#)')
def links = new ArrayList()
matches.each {
def path = it - 'href="' - '"' - '#'
if(!new File(file.getParentFile(), path).exists()) {
links.add(path)
}
}
if(!links.isEmpty()) {
errorlinks.put(file.path, links)
}
}
運行時傳入一個地址。如果是HTML文件,則檢查該文件。如果是目錄,則檢查里面的HTML文件,其他文件不檢查。然后把有錯誤的文件及其超鏈接在最后打印出來,正確的不打印。
原文:http://blog.csdn.net/maosidiaoxian/article/details/40685237
總結
以上是生活随笔為你收集整理的html检查链接正确代码,Groovy脚本检查html坏链接的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 简单安装ELK分析日志及使用心得
- 下一篇: Linux SSH远程管理故障如何排查?
