html检查链接正确代码,Groovy脚本检查html坏链接
這些天在搞Gradle翻譯,因?yàn)樵g者在翻譯的同時(shí)也把文件進(jìn)行了整理,并且把翻譯過(guò)的章節(jié)放到新的文件夾中,導(dǎo)致可能有些超鏈接未改正過(guò)來(lái)變成死鏈接。
本想在網(wǎng)上找個(gè)工具來(lái)檢查的,百度了幾個(gè)工具要么太大要么要安裝,懶得弄那么多,于是用Groovy寫了一個(gè)腳本。此腳本僅檢查本地超鏈接,代碼如下:
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)
}
}
運(yùn)行時(shí)傳入一個(gè)地址。如果是HTML文件,則檢查該文件。如果是目錄,則檢查里面的HTML文件,其他文件不檢查。然后把有錯(cuò)誤的文件及其超鏈接在最后打印出來(lái),正確的不打印。
原文:http://blog.csdn.net/maosidiaoxian/article/details/40685237
總結(jié)
以上是生活随笔為你收集整理的html检查链接正确代码,Groovy脚本检查html坏链接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简单安装ELK分析日志及使用心得
- 下一篇: 计算机科学与技术研究目的,计算机科学与技