首页 > Programe, tips > How do I extract URLs from html?

How do I extract URLs from html?

2009年11月23日 哈哈 发表评论 阅读评论

perl的faq第九节有个简洁的例子

#!/usr/bin/perl -n00
# qxurl – tchrist@perl.com
print “$2\n” while m{
< \s* A \s+ HREF \s* = \s* (["']) (.*?) \1 \s* >
}gsix;

这个正则表达式相当简单实用
但是实际上有点问题
比如
<a href=”xxxxxxx” target=”_blank”>
这样的html代码
就会匹配出xxxxxxx” target=”_blank
这显然不行
于是做了小小的改动:

#!/usr/bin/perl -n00
# qxurl – tchrist@perl.com
print “$2\n” while m{
< \s* A \s+ HREF \s* = \s* (["']) (.*?) \1 \s* [^>]*
\s* >
}gsix;

这样就没问题了

原创文章,转载请注明: 转载自嘻嘻哈哈的部落格(blog)

本文链接地址: How do I extract URLs from html?

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word