早晨看了切尔西和利物浦的比赛才睡觉,一觉直接到11点。起来洗漱,吃饭,中午12点,3天后,我又回到了实验室……
昨天提到了球赛比分短信提醒,花了一下午的时间才搞定,真汗~代码如下(commented here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/usr/bin/perl -w use Encode; use HTML::TreeBuilder; use Data::Dumper qw(Dumper); $Data::Dumper::Indent = 1; undef $/; $oldscore=$score=0; $times = 0; for ( $times=0;$times <= 50; $times++){ system("wget","http://score.espnstar.com.cn/","-Oindex.html"); open($fd," $string = <$fd>; $string = decode("gb2312", $string); $string = encode("utf8", $string); $tree = HTML::TreeBuilder->new_from_content($string); foreach $row ( $tree->find_by_tag_name("tr") ) { $line = ""; foreach $cell ( $row->content_list ) { $line = $line.$cell->as_text." "; } if ($line =~ /拜仁慕尼黑/gs) { if ( $line =~ /(\d*) - (\d*)/gs) { $score= $1.$2; } if($score ne $oldscore){ $cmd='curl -x 222.171.7.131:7628 -u user@163.com:passwd -d status="'.$line.'"'." " .'http://twitter.com/statuses/update.xml'; system($cmd); $oldscore=$score; } } } $tree->delete(); close($fd); system("rm","index.html"); sleep 300; } |
为什么花了那么长时间,主要原因是PERL的熟悉程度太差了,经常需要上网查资料,PERL也看了好久了,不过用得太少了,真郁闷,看着这么像C的代码……还有就是正则表达式,本来想自己构造正则表达式来提取信息的,但是玩着玩着才发现,实在太难了,恰巧前几天看了一段话:“一些人,遇到一个问题时就想:“我知道,我将使用正则表达式。”现在他有两个问题了。 –Jamie Zawinski, in comp.emacs.xemacs ”说的很好~