免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2771 | 回复: 2
打印 上一主题 下一主题

请帮忙 html::parser ,从mp3.baidu.搜索mp3 [复制链接]

求职 : 数据库管理员
论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-01 22:03 |只看该作者 |倒序浏览
根据HTML::LinkExtor修改过来的package

1.<a href=http://aa.aa/>; <img=aa.img alt="asdf">;</a>;
2.<a href=http://aa.aa/>; fffff</a>;
3 .<a href=http://aa.aa/>;test <font size=12>;text2</font>; </a>;
第一,二种可以
第三种只能抓到 test ,后面的 text2抓不到,
请帮忙

刚学perl不久,玩玩而已,呵呵
          
perl 真是个好东西


  1. package myLinkExtor;

  2. # $Id: LinkExtor.pm,v 1.33 2003/10/10 10:20:56 gisle Exp $

  3. require HTML::Parser;
  4. @ISA = qw(HTML::Parser);
  5. $VERSION = sprintf("%d.%02d", q$Revision: 1.33 $ =~ /(\d+)\.(\d+)/);


  6. use strict;
  7. use HTML::Tagset ();

  8. # legacy (some applications grabs this hash directly)
  9. use vars qw(%LINK_ELEMENT);
  10. *LINK_ELEMENT = \%HTML::Tagset::linkElements;


  11. sub new
  12. {
  13.     my($class, $cb, $base) = @_;
  14.     my $self = $class->;SUPER::new(
  15.                     start_h =>; [\&a_start_handler, "self,tagname,attr"],
  16.                     report_tags =>; [qw(a img)],
  17.                );
  18.    
  19.     if ($base) {
  20.         require URI;
  21.         $self->;{extractlink_base} = URI->;new($base);
  22.     }
  23.     $self;
  24. }


  25. sub _found_link
  26. {
  27.     my $self = shift;
  28.     push(@{$self->;{'links'}}, [@_]);
  29.    
  30. }


  31. sub links
  32. {
  33.     my $self = shift;
  34.     exists($self->;{'links'}) ? @{delete $self->;{'links'}} : ();
  35. }

  36. # We override the parse_file() method so that we can clear the links
  37. # before we start a new file.
  38. sub parse_file
  39. {
  40.     my $self = shift;
  41.     delete $self->;{'links'};
  42.     $self->;SUPER::parse_file(@_);
  43. }



  44. ######
  45. ##
  46. #
  47. #my declare
  48. #



  49. sub a_start_handler
  50. {
  51.     my($self, $tag, $attr) = @_;
  52.     return unless $tag eq "a";
  53.     return unless exists $attr->;{href};

  54.     $self->;handler(text  =>; [], '@{dtext}' );

  55.     push(@{$self->;handler("text")}, "href" );
  56.     push(@{$self->;handler("text")}, $attr->;{href} );

  57.     $self->;{'tag'}=$tag ;
  58.    
  59.    
  60.     $self->;handler(start =>; \&img_handler);
  61. #    $self->;handler(end   =>; \&a_end_handler, "self,tagname");
  62.     $self->;handler(end   =>; \&a_end_handler, "self,tagname");


  63. }

  64. sub img_handler
  65. {
  66.     my($self, $tag, $attr) = @_;
  67.     return unless (($tag eq "img") or ($tag eq "font")) ;
  68.      
  69.     push(@{$self->;handler("text")}, $attr->;{alt} || "[IMG]");

  70. }

  71. sub a_end_handler
  72. {
  73.     my($self, $tag) = @_;
  74. #   my $text = join("", @{$self->;handler("text")});
  75. #   $text =~ s/^\s+//;
  76. #   $text =~ s/\s+$//;
  77. #   $text =~ s/\s+/ /g;

  78.     $self->;_found_link($self->;{'tag'} ,@{$self->;handler("text")}) ;

  79.     $self->;handler("text", undef);
  80.     $self->;handler("start", \&a_start_handler);
  81.     $self->;handler("end", undef);
  82. }

复制代码



findmp3.pl
可按歌手搜索mp3列表,
因为不好判断有多少页,就默认5 可传入参数改变,





  1. #!/usr/bin/perl
  2. #
  3. #Coder : Gan Jian Hui
  4. #
  5. #Last Modify :2005-07-29
  6. #
  7. use LWP::UserAgent;
  8. use URI::URL ;
  9. use strict ;
  10. use myLinkExtor;
  11. use URI::Escape;


  12. if (@ARGV <= 0)
  13. {
  14.         print STDERR "usage: get liu de hua \n";
  15.         exit(1) ;
  16. }

  17. my ($word,$maxpage) =@ARGV ;
  18. $maxpage=$maxpage || 5 ;
  19. $word=uri_escape($word) ;

  20. my $browser = LWP::UserAgent->;new;
  21. $browser->;env_proxy;
  22. $browser->;agent('Mozilla/5.0');

  23. my $p = myLinkExtor->;new() ;

  24. my $i=0 ;
  25. my $fsurl ;
  26. for ($i=0; $i<$maxpage; $i++)
  27. {
  28.         $fsurl = &findnext($i,$word);
  29.         &geturl($browser, $p, $fsurl) ;
  30. }

  31. sub geturl()
  32. {       
  33.         my ($bs, $lkextor , $url) = @_ ;
  34.                
  35.         my $req=HTTP::Request->;new(GET =>; $url) ;
  36.         my $response = $bs->;request($req) ;
  37.         $lkextor->;parse($response->;content) ;

  38.         my $base = $response->;base;
  39.        
  40.         my $nurl ;

  41. #        <a href=aa.htm>;asdf</a>;
  42. #         0  1    2      3
  43.         foreach my $link  ($lkextor->;links)
  44.         {
  45.                 next if @$link[0] ne 'a' ;
  46.                 $nurl = url(@$link[2],$base)->;abs ;
  47.                 shift (@$link) ;
  48.                 shift (@$link) ;
  49.                 shift (@$link) ;

  50.                 if ($nurl =~ /.(mp3|rm|wma)$/ )
  51.                 {
  52.                         my $text = join("",@$link)  ;
  53.                         $text =~ s/^\s+//;
  54.                         $text =~ s/\s+$//;
  55.                         $text =~ s/\s+/ /g;
  56. #   
  57.                         print $nurl."\t " . $text ."\n" ;
  58.                 }
  59.        
  60.         }
  61. }



  62. sub findnext(){
  63.         my ($page,$WORD) = @_ ;
  64.         my $RM=30 ;
  65.         my $PM=$RM*$page ;
  66.         my $MAXPM=100 ;
  67.         my $NL=  "http://mp3.baidu.com/m?z=0&cl=3&ct=134217728&sn=&lm=-1&cm=1&sc=1&bu=&rn=30&tn=baidump3&word=$WORD&pn=$PM" ;
  68.         return  $NL ;       
  69. }


复制代码

求职 : 数据库管理员
论坛徽章:
0
2 [报告]
发表于 2005-08-01 23:00 |只看该作者

请帮忙 html::parser ,从mp3.baidu.搜索mp3

可能是我搞错了,
好像可以输出

论坛徽章:
0
3 [报告]
发表于 2005-08-04 18:09 |只看该作者

请帮忙 html::parser ,从mp3.baidu.搜索mp3

第三种URL, <a href="#">;text1 <font color=#>;text2</font>; text3</a>; 用HTML::TreeBuilder可以解决



  1. #!/usr/bin/perl -w

  2. use strict;
  3. use HTML::TreeBuilder;
  4. sub extract_link_content($);

  5. my $root = HTML::TreeBuilder->;new_from_file("./link.html");
  6. my @links = $root->;find_by_tag_name('a');
  7. map { print $_->;attr('href'), "\t", extract_link_content($_), "\n"; } @links;

  8. sub extract_link_content($)
  9. {
  10.         my $link = shift;
  11.         my $text = "";
  12.         my @content = $link->;content_list;
  13.         for (my $i = 0; $i <= $#content; ++$i)
  14.         {
  15.                 $text .= (ref($content[$i]) eq 'HTML::Element' ?
  16.                                         extract_link_content($content[$i]) : $content[$i]);
  17.         }

  18.         return $text;
  19. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP