我正在使用wget
下载 HTML 页面并从中提取信息。具体来说,我想改变这个:
<a href="/312728/" title="The 10 Best Goals ever">
<a href="/671921/" title="Golf at its best">
<a href="/371285/" title="Football Legends">
<a href="/576903/" title="Boxing Legends">
进入此并另存为txt文件。
/312728/The 10 Best Goals ever
/671921/Golf at its best
/371285/Football Legends
/576903/Boxing Legends
我已经尝试过:
wget --quiet -O - http://some-site.com | grep -o '<a href="/?/" title="?"> > new.txt
但这并没有给我想要的结果。
最佳答案
将 grep
更改为 egrep
以获得更精细的正则表达式功能,您可以执行以下操作:
wget --quiet -O - http://some-site.com | egrep -e '<a href="\/[0-9]*\/" title="[:alnum:]*'
应该返回:
<a href="/312728/" title="The 10 Best Goals ever">
<a href="/671921/" title="Golf at its best">
<a href="/371285/" title="Football Legends">
<a href="/576903/" title="Boxing Legends">
然后,使用 awk
我们可以用双引号分隔这些内容并挑选出您想要返回的部分:
wget --quiet -O - http://some-site.com | egrep -e '<a href="\/[0-9]*\/" title="[:alnum:]*' | awk -F'"' '{print $2$4}'
应该返回这个:
/312728/The 10 Best Goals ever
/671921/Golf at its best
/371285/Football Legends
/576903/Boxing Legends
您可以重定向到这样的文本文件:
wget --quiet -O - http://some-site.com | egrep -e '<a href="\/[0-9]*\/" title="[:alnum:]*' | awk -F'"' '{print $2$4}' >> mytextfile.txt
关于html - 使用命令行从 HTML 文档中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36163189/