2009年4月1日星期三

[PerlChina] Re: 新手请教一个正则表达式的问题

On Wed, 2009-04-01 at 03:43 -0700, William wrote:
> 根据学习 自己写了一个抓取搜索引擎结果中URL和关键字信息的perl程序,如下
> 问题是,对于URL的处理一直没有解决,搜索到一个正则匹配,但是没有想明白
> 在程序的26行,这个匹配结果是带有href等htm标签的,但是打印出来却没有,只剩下很完美的URL
> 尝试获取关键字信息,却无法剥离如font size等的htm字段
> @urls=($response->content =~ /" href="(http.*?)" target="_blank"/g);
> @titles=($response->content =~ /font size="3">\".*?\"</g);
去掉括号试试
> ----------------------不华丽的分隔符,以下是程序------------------------------
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use LWP::UserAgent;
>
> print "Please enter a keyword: ";
> chomp(my $keyword=<STDIN>);
> print "Please enter the number you want: ";
> chomp(my $nums=<STDIN>);
>
> my @all_urls;
> my @urls;
> my @titles;
> my @all_titles;
> for (my $i=0;$i<($nums/10);$i++) {
> my $start=$i*10;
> # my $url="http://www.google.cn/search?hl=zh-CN&newwindow=1&q=
> $keyword&start=$start&sa=N";
> my $url="http://www.baidu.com/s?wd=$keyword";
>
> my $agent=LWP::UserAgent->new;
> my $request=HTTP::Request->new(GET=>$url);
> $request->header('User-Agent'=>'Mozilla/4.0 (compatible; MSIE 7.0;
> Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)');
> my $response=$agent->request($request);
> if ($response->is_success) {
> # @urls=($response->content =~ /class=r><a href=\"(http.*?)\"
> target=_blank/g);
> @urls=($response->content =~ /" href="(http.*?)" target="_blank"/
> g);
> @titles=($response->content =~ /font size="3">\".*?\"</g);
> }
>
> @all_urls=(@all_urls,@urls);
> @all_titles=(@all_titles,@titles)
> }
>
> my $all=join "\n",@all_urls;
> open ALL,">result.txt";
> print ALL $all;
> close(ALL);
> >


--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina Mongers 讨论组"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

没有评论: