分页: 1 / 1

提问: 怎样用vim提取网页源文件中符合要求的超链接。

发表于 : 2010-12-07 14:53
weiidoo
比如把: http://bbs.ubuntu.org.cn/10位数字.htm
格式的都留下并以换行显示,其他内容都删掉。
因为业务经常会有这种需求,不知道万能的VI,能否满足我小小的要求 :em04

Re: 提问: 怎样用vim提取网页源文件中符合要求的超链接。

发表于 : 2010-12-07 15:03
eexpress

代码: 全选

● cat ~/bin/unused-script/fetch-link.pl 
#!/usr/bin/perl

use LWP::UserAgent;
my $url=shift;
#my $url='http://forum.ubuntu.org.cn/search.php?search_id=newposts';
#my $url='http://doc.linuxpk.com/2970.html';
my $ua=new LWP::UserAgent();
my $re= $ua->get($url);
die if (!$re->is_success);
my $html= $re->content;

#得到页面中所有链接
while($html=~m{<a href=(["'])(.*?)\1.*?>(<.*?/>)*(.*?)</a>}gsi){
#print "$2\t--->$4\n";
my $l=$2; my $t=$4;if($l=~/^http/ and $l!~/com\/$|cn\/$/ and $t!~/^</){print "$l\t--->$t\n";}
}
万能的是pl

Re: 提问: 怎样用vim提取网页源文件中符合要求的超链接。

发表于 : 2010-12-07 15:30
风间星魂

正则表达式
无论是vim正则还是shell perl python都行。

Re: 提问: 怎样用vim提取网页源文件中符合要求的超链接。

发表于 : 2010-12-07 15:55
weiidoo
谢谢2楼! 我学习一下

Re: 提问: 怎样用vim提取网页源文件中符合要求的超链接。

发表于 : 2010-12-07 16:37
lilydjwg
二楼真麻烦。

代码: 全选

grep -oP 'http://bbs\.ubuntu\.org\.cn/\d{10}\.htm'
这种东西,你首先得会正则表达式,其次学下 sed grep awk sort uniq 等文本处理工具。Perl 更强大,但是很难学。Python/Ruby 之类的对付复杂的处理也很不错。