我想解析下载网页的来源以获取链接列表。单线可以很好地工作。以下是我迄今为止所尝试的内容:使用awk sed或grep从网页源解析网址
这似乎忽略了某些网页名称的部分网址。
$ cat file.html | grep -o -E '\b(([\w-]+://?|domain[.]org)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))'|sort -ut/ -k3
这将获得所有的URL,但我不想包含具有锚链接的链接。此外,我希望能够指定domain.org/folder/:
$ awk 'BEGIN{
RS="</a>"
IGNORECASE=1
}
{
for(o=1;o<=NF;o++){
if ($o ~ /href/){
gsub(/.*href=\042/,"",$o)
gsub(/\042.*/,"",$o)
print $(o)
}
}
}' file.html
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – 2011-03-20 15:14:46
'grep -E'不理解非捕获子模式或' \ w'在字符类中转义。你需要使用'grep -P'。 – 2011-03-20 16:17:29
@丹尼斯威廉姆森:现在返回的结果与第二个例子类似,但我需要能够清除锚链接并指定http://domain.com/folder – Astron 2011-03-20 16:21:40