#!/usr/bin/perl -w
use strict;
use warnings;
use LWP::UserAgent;
use XML::Simple;
use Data::Dumper;
use HTML::TableExtract;
my %GbHsPtr;
GetHtml();
Exp2Exl();
sub GetHtml{
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1");
my $req = HTTP::Request->new(POST => 'http://www.taifex.com.tw/chinese/3/7_12_3.asp');
my $res = $ua->request($req);
return unless $res->is_success;
#print $res->content();
my $html_string = $res->content();
my $te = HTML::TableExtract->new( depth => 4, count => 0);
$te->parse($html_string);
my $cot=0;
my ($ID,$NM,$BYER,$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM);
my ($NID,$NNM);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
if($mon<10){ $mon="0".$mon; }
if($mday<10){ $mday="0".$mday;}
my $oMyFile = "../rem/".$year."_".$mon."_".$mday.".csv";
open (oMyFilePtr,">$oMyFile") || die "open oMyFile(Rem) Error";
foreach my $ts ($te->tables) {
foreach my $row ( $ts->rows ) {
my $st = join('.',@{$row});
$st =~ s/\,//g; $st =~ s/\s+//g; $st=~ s/\./\,/g;
printf oMyFilePtr ($st."\n");
if($cot>=3 && $cot<=$#{$ts->rows}-4){
($ID,$NM,$BYER,$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM) = split(',',$st);
if( $ID || $NM ){ ($NID,$NNM)=($ID,$NM); }
else{ ($ID,$NM) = ($NID,$NNM); }
$GbHsPtr{$NM}{$BYER} = {
"TBC" => $TBC,
"TBM" => $TBM,
"TSC" => $TSC,
"TSM" => $TSM,
"TBSC" => $TBSC,
"TBSM" => $TBSM,
"RBC" => $RBC,
"RBM" => $RBM,
"RSC" => $RSC,
"RSM" => $RSM,
"RBSC" => $RBSC,
"RBSM" => $RBSM,
};
}
$cot++;
}
}
# print Dumper (\%GbHsPtr);
}
sub Exp2Exl {
my ($NM,$BYER,$file,$by);
my %lt = (
"臺股期貨" => "txf",
"電子期貨" => "exf",
"金融期貨" => "fxf",
"小型臺指" => "mxf",
);
my %By = (
"自營商" => "internal_bank",
"投信" => "internal_stock",
"外資及陸資" => "external_stock",
);
my $oMyfile;
foreach ( keys %GbHsPtr ){
$NM =$_;
if( defined($lt{$NM})){
$file = $lt{$NM};
foreach ( keys %{$GbHsPtr{$NM}} ){
$BYER = $_;
$by = $By{$BYER};
$oMyfile = "../rem/".$file."/".$by.".csv";
open(oMyFilePtr,">>$oMyfile") || die "open $oMyfile Error\n";
my $TBC = $GbHsPtr{$NM}{$BYER}{"TBC"};
my $TBM = $GbHsPtr{$NM}{$BYER}{"TBM"};
my $TSC = $GbHsPtr{$NM}{$BYER}{"TSC"};
my $TSM = $GbHsPtr{$NM}{$BYER}{"TSM"};
my $TBSC = $GbHsPtr{$NM}{$BYER}{"TBSC"};
my $TBSM = $GbHsPtr{$NM}{$BYER}{"TBSM"};
my $RBC = $GbHsPtr{$NM}{$BYER}{"RBC"};
my $RBM = $GbHsPtr{$NM}{$BYER}{"RBM"};
my $RSC = $GbHsPtr{$NM}{$BYER}{"RSC"};
my $RSM = $GbHsPtr{$NM}{$BYER}{"RSM"};
my $RBSC = $GbHsPtr{$NM}{$BYER}{"RBSC"};
my $RBSM = $GbHsPtr{$NM}{$BYER}{"RBSM"};
printf oMyFilePtr ("$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM\n");
}
}
}
}
或者你可以下載打包好的 Path,
會有 code, rem, cur的資料夾
%code : 程式碼
%cur : 每日的 1分K 線 (可以自行定義成 5分K線)
%rem : 成交筆數跟未平倉Data
使用步驟
%source fu_ticket.sh
Download
http://sites.google.com/site/funningboy/perl_code/future_v1.rar?attredirects=0&d=1
use strict;
use warnings;
use LWP::UserAgent;
use XML::Simple;
use Data::Dumper;
use HTML::TableExtract;
my %GbHsPtr;
GetHtml();
Exp2Exl();
sub GetHtml{
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1");
my $req = HTTP::Request->new(POST => 'http://www.taifex.com.tw/chinese/3/7_12_3.asp');
my $res = $ua->request($req);
return unless $res->is_success;
#print $res->content();
my $html_string = $res->content();
my $te = HTML::TableExtract->new( depth => 4, count => 0);
$te->parse($html_string);
my $cot=0;
my ($ID,$NM,$BYER,$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM);
my ($NID,$NNM);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
if($mon<10){ $mon="0".$mon; }
if($mday<10){ $mday="0".$mday;}
my $oMyFile = "../rem/".$year."_".$mon."_".$mday.".csv";
open (oMyFilePtr,">$oMyFile") || die "open oMyFile(Rem) Error";
foreach my $ts ($te->tables) {
foreach my $row ( $ts->rows ) {
my $st = join('.',@{$row});
$st =~ s/\,//g; $st =~ s/\s+//g; $st=~ s/\./\,/g;
printf oMyFilePtr ($st."\n");
if($cot>=3 && $cot<=$#{$ts->rows}-4){
($ID,$NM,$BYER,$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM) = split(',',$st);
if( $ID || $NM ){ ($NID,$NNM)=($ID,$NM); }
else{ ($ID,$NM) = ($NID,$NNM); }
$GbHsPtr{$NM}{$BYER} = {
"TBC" => $TBC,
"TBM" => $TBM,
"TSC" => $TSC,
"TSM" => $TSM,
"TBSC" => $TBSC,
"TBSM" => $TBSM,
"RBC" => $RBC,
"RBM" => $RBM,
"RSC" => $RSC,
"RSM" => $RSM,
"RBSC" => $RBSC,
"RBSM" => $RBSM,
};
}
$cot++;
}
}
# print Dumper (\%GbHsPtr);
}
sub Exp2Exl {
my ($NM,$BYER,$file,$by);
my %lt = (
"臺股期貨" => "txf",
"電子期貨" => "exf",
"金融期貨" => "fxf",
"小型臺指" => "mxf",
);
my %By = (
"自營商" => "internal_bank",
"投信" => "internal_stock",
"外資及陸資" => "external_stock",
);
my $oMyfile;
foreach ( keys %GbHsPtr ){
$NM =$_;
if( defined($lt{$NM})){
$file = $lt{$NM};
foreach ( keys %{$GbHsPtr{$NM}} ){
$BYER = $_;
$by = $By{$BYER};
$oMyfile = "../rem/".$file."/".$by.".csv";
open(oMyFilePtr,">>$oMyfile") || die "open $oMyfile Error\n";
my $TBC = $GbHsPtr{$NM}{$BYER}{"TBC"};
my $TBM = $GbHsPtr{$NM}{$BYER}{"TBM"};
my $TSC = $GbHsPtr{$NM}{$BYER}{"TSC"};
my $TSM = $GbHsPtr{$NM}{$BYER}{"TSM"};
my $TBSC = $GbHsPtr{$NM}{$BYER}{"TBSC"};
my $TBSM = $GbHsPtr{$NM}{$BYER}{"TBSM"};
my $RBC = $GbHsPtr{$NM}{$BYER}{"RBC"};
my $RBM = $GbHsPtr{$NM}{$BYER}{"RBM"};
my $RSC = $GbHsPtr{$NM}{$BYER}{"RSC"};
my $RSM = $GbHsPtr{$NM}{$BYER}{"RSM"};
my $RBSC = $GbHsPtr{$NM}{$BYER}{"RBSC"};
my $RBSM = $GbHsPtr{$NM}{$BYER}{"RBSM"};
printf oMyFilePtr ("$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM\n");
}
}
}
}
PS: 底下轉檔出現亂碼部分要改成###之後的版本才對
回覆刪除#原始檔
my %lt = (
"臺股期貨" => "txf",
"電子期貨" => "exf",
"金融期貨" => "fxf",
"小型臺指" => "mxf",
);
my %By = (
"自營商" => "internal_bank",
"投信" => "internal_stock",
"外資及陸資" => "external_stock",
);
#################################################
#更新檔
my %lt = (
"臺股期貨" => "txf",
"電子期貨" => "exf",
"金融期貨" => "fxf",
"小型臺指" => "mxf",
);
my %By = (
"自營商" => "internal_bank",
"投信" => "internal_stock",
"外資及陸資" => "external_stock",
);
Bug report
回覆刪除"小型臺指" => "mxf",
請改成
"小型臺指期貨" => "mxf",
THX