# 抓取網頁中的 link use LWP; use HTML::TreeBuilder; use Text::Iconv; # 取得命令列中的網頁位址,放入 $url 中 #my $url=shift || die "您沒有輸入 url 網址!\n"; my $stock=2357; if(defined $ARGV[0]){ $stock=$ARGV[0]; } my $url="http://tw.stock.yahoo.com/d/s/dividend_".$stock.".html"; my $ua=LWP::UserAgent->new; my $res=$ua->get($url); die "Can't get $url ", $res->status_line unless $res->is_success; my $html=$res->content; $converter = Text::Iconv->new("big5", "utf8"); $html = $converter->convert("$html"); #print "$html\r\n"; my $root=HTML::TreeBuilder->new_from_content($html); my @names=$root->look_down(_tag=>'TITLE'); foreach my $line(@names){ my $name=$line->as_trimmed_text; my @items=split(/\-/,$name); print $items[0],",",$items[1],",\r\n"; } my @links=$root->look_down( _tag=>'table', ); for($j=0; $j<=$#links; $j++){ if($j == 9){ #print "\t$j=", $links[$j]->attr('href'),' ', $links[$j]->as_trimmed_text, "\r\n"; my @items=$links[$j]->look_down(_tag=>'tr'); #print "Item $j\r\n"; foreach my $item(@items){ my @datas=$item->look_down(_tag=>'td'); foreach my $data(@datas){ my $tmp=$data->as_trimmed_text; $tmp =~ s/\ //ge; $tmp =~ s/\ //ge; print $tmp,","; } print "\r\n"; #print "$j\t", $item->as_trimmed_text,"\r\n"; } #print "$j=", $links[$j]->as_trimmed_text, "\r\n"; } } $root->delete;
這個程式是先跑去抓某個網頁的股票代碼列表(有點舊)然後再去呼叫上面那一支程式把歷史股利政策全抓下來。
# 抓取網頁中的 link use LWP; use HTML::TreeBuilder; use Text::Iconv; # 取得命令列中的網頁位址,放入 $url 中 #my $url=shift || die "您沒有輸入 url 網址!\n"; my $url="http://www.tej.com.tw/webtej/doc/uid.htm"; my $ua=LWP::UserAgent->new; my $res=$ua->get($url); die "Can't get $url ", $res->status_line unless $res->is_success; my $html=$res->content; $converter = Text::Iconv->new("big5", "utf8"); $html = $converter->convert("$html"); #print "$html\r\n"; my $root=HTML::TreeBuilder->new_from_content($html); my @links=$root->look_down( _tag=>'td' ); #my @links=$root->query('tr'); for($j=0; $j<=$#links; $j++){ my $tmp=$links[$j]->as_trimmed_text; #my $tmp=$links[$j]->attr('str'); $tmp=~s/\D//ge; if(($tmp < 9999)&&($tmp ne "")){ #print $tmp, "\r\n"; my $var=`perl yahooStock01.pl $tmp`; print "$var\n"; } } $root->delete;
沒有留言:
張貼留言