驽马十驾

给我智慧,让我知道我真正的利益所在; 给我坚定的意志,让我能够按照智慧的引导行事
随笔 - 10, 文章 - 1, 评论 - 1, 引用 - 0
数据加载中……

perl写刷火车票的小程序,有空完善一下

  use Encode;
  use LWP::Simple;

  sub URLEncode {
    my $theURL = $_[0];
    my $MetaChars = quotemeta( ';,/?\|=+)(*&^%$#@!~`:');
    $theURL =~ s/([$MetaChars\"\'\x80-\xFF])/"%" . uc(sprintf("%2.2x",         ord($1)))/eg;
    $theURL =~ s/ /\+/g;
    return $theURL;
  }
  my $start = '北京';
  my $dest = '莱阳';
  my $date = '2009-1-18';
  my $url = 'http://www.huochepiao.com/City/Search.asp?leixing=' .
              URLEncode('转让'). '&chufa=' . URLEncode($start) . '&daoda=' . URLEncode($dest);
  #print $url;

  my %result=();


  #print $content;
while(1)
{
  my $content = get $url;
  die "Couldn't get $url" unless defined $content;
  while($content =~ /href=([^ ]*htm)\s*target=_blank>[^<]*$start-$dest[^<]*$date<\/a>/g )
  {
     my $info="";
     my $inurl = $1;
     if(!defined($result{$1})) {
       print $1 . "\n";
       my $content2 = get $1;
       #print $content2;
       $content2 =~ /车 次:\s*<a href=[^>]*><b>([^><]*)/;
       $info .= "车次: $1\n";
       $content2 =~ /席 别:\s*([^<]*)<br>/;
       $info .= "席 别:$1\n";
       $content2 =~ /始发站:\s*([^<]*)<br>/;
       $info .= "始发站:$1\n";
       $content2 =~ /到达站:\s*([^<]*)<br>/;
       $info .= "到达站:$1\n";
       $content2 =~ /发布时间:\s*([^<]*)<\/TD>/;
       $info .= "发布时间:$1\n";
       $content2 =~ /发布人:\s*([^<]*)<a href="[^<>]*">/;
       $info .= "发布人:$1\n";
       $content2 =~ /电 话:\s*<a href="[^<>]*">([^<]*)<\/a>/;
       $info .= "电话:$1 \n";
       print $info;
       print "-------------------\n";
       $result{$inurl} = $info;
     }
  }
  print "\n";
 }

posted on 2009-01-18 23:23 驽马 阅读(429) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。