自动替换HTML页面中的所有无效链接文本

时间:2018-07-20 07:12:15

标签: html regex shell curl automation

我每天都有一个动态创建的固件下载页面,其中包含许多不同的下载链接:https://freifunk.in-kiel.de/firmware/release-candidate/2018.1~exp-215/site/download/

现在并不是所有的下载每天都有效,但是那时候仍然需要注意,这些固件是不适用的。

我考虑过通过正则表达式解析脚本以获取该页面上的所有链接,然后启动一些curl调用,以检查链接是否失效。如果已失效,则将链接的文本替换为“ n / a”。

1 个答案:

答案 0 :(得分:0)

此脚本成功检查了所有包含字符串“ gluon”的链接:

#!/bin/bash
# set the new version here
CUR=2018.1~ngly-234
BRANCH='nightly'
OUT_FILE=index.html
wget -k --no-check-certificate http://freifunk.in-kiel.de/firmware-rc.html -O $OUT_FILE
# replace the data from the template
sed -i 's|/sysupgrade/gluon-ffki-<VERSION>|/sysupgrade/gluon-ffki-'$CUR'|g' $OUT_FILE
sed -i 's|/factory/gluon-ffki-<VERSION>|/factory/gluon-ffki-'$CUR'|g' $OUT_FILE
sed -i 's|release-candidate|'$BRANCH'/'$CUR'|g' $OUT_FILE
echo -n "dead link check "
#sed -i "s/tube2/nixtube2/g" $OUT_FILE  # for debug to create a dead link
INVALID='">n/a </a><deadlink none="'
while IFS= read -r URL; do
  if wget --no-check-certificate --spider "$URL" 2>/dev/null; then
    echo -n .
  else
    echo
    echo "$URL does not exist"
    sed -i 's|'$URL'|'$URL''"$INVALID"'|g' $OUT_FILE
  fi
#done < <(grep -Po '(?<=href=")[^"]*' $OUT_FILE|grep gluon|grep alfa)  # for debug
done < <(grep -Po '(?<=href=")[^"]*' $OUT_FILE|grep gluon)
echo "dead link check done"
sed -i 's|http://|//|g' $OUT_FILE