博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从html文件中提取超链接URL的shell脚本
阅读量:2387 次
发布时间:2019-05-10

本文共 873 字,大约阅读时间需要 2 分钟。

sed -'/<a /p' html.txt | sed 's#<a \([^>]*\)>#--SYN--\1--FIN--#g; s/<//g; s/>//g' | \
sed 's/--SYN--/</g; s/--FIN--[^<]*</></g; s/[^<]*</</; s/--FIN--.*/>/;' | \
sed "s#<[^>]*href=\([^a-zA-Z>]*http://[^ >]*\)[^>]*># @\1@#g; s/<[^>]*>//g; s/'//g; s/@/ /g" > url.txt


这里提取的是 <a href="http://domain/path/to/html.html"> 中的 http://domain/path/to/html.html

也就是

1、只匹配html的Tag为a的节点。

2、选择的是href的值。

3、href的值需要使用http://开头,就是说不支持相对路径。


写成sed脚本可以表示为:

# this script is use to dig href url from html file
s/<a \([^>]*\)>/--SYN--\1--FIN--/g;
s/[><]//g;
s/--FIN--/>/g;
s/--SYN--/</g;
s/^\(.*\)$/>\1</;
s/>[^<]*</></g;
s#<[^>]*href=[^a-zA-Z>]*\(http://[^ >]*\)[^>]*>#@\1@#g;
s/<[^>]*>//g;
s/@@/\
/g;
s/[><
'"@]//g;

/^ *$/d;


sed脚本2:

:a;
h;
s@^[^<]*<a\s*[^>]*\s*href\s*=\s*['"]*\(http://[^> "']*\)[^>]*>.*@\1@p;
g;
s@<[a-zA-Z/][a-zA-Z]*[^>]*>@@;
t a;
/<[a-zA-Z\/][a-zA-Z]*[^>]*$/{

N; b a; };
d;

转载地址:http://elsab.baihongyu.com/

你可能感兴趣的文章
php变量引用和计数_refcount_gc和is_ref_gc
查看>>
windows环境下php和Php扩展编译,扩展dll文件编译
查看>>
magento 验证码
查看>>
magento性能优化系列二:db篇
查看>>
Discuz!$_G变量的使用方法
查看>>
magento memcache缓存配置
查看>>
PHP json_encode中文乱码解决方法
查看>>
mysql服务启动、关闭
查看>>
php获取中文字符串的首字符拼音字母
查看>>
php curl通过代理获取数据
查看>>
6 个 Linux性能监控命令行工具
查看>>
mysql 编码字符集配置
查看>>
php查看opcode编码的扩展 opdumper
查看>>
php转换html格式为文本格式
查看>>
mysql-proxy主从服务架构下读写分离和负载均衡实现及原理
查看>>
Nginx location 和 rewrite retry
查看>>
基于nginx的FastCGI的缓存配置
查看>>
Nginx模块fastcgi_cache的几个注意点
查看>>
PHP使用curl伪造IP地址和header信息
查看>>
代理服务器中的HTTP代理与SOCKS代理有什么区别?
查看>>