|
测试开发需要测试人员掌握不止一种高级语言、脚本语言,以便根据需要选择最佳的实现方式。
语言脚本不经常用就会慢慢遗忘,上班抽空自己写写小工具练手,防止生疏了。
最近用perl写了个基金净值小工具。
USAGE:./xxx (-c) (000021 XXXXXX XXXXXX XXXXXX)
不带参数时,从数据库读取需要采集以及处理的基金号码(type1为普通基金,2为货币基金)
带参数,且不含-c时,将参数中的基金号码当做普通基金处理
带参数,且包含-c时,将参数中的基金号码当做货币基金处理
数据采集代码如下
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use LWP::UserAgent;
use DBI;
my @ids;
my @crashids;
###connect database
my $dbh = DBI -> connect('dbi:mysql:pltest:localhost','xxxxx','xxxxxx');
if (!defined $dbh)
{
die "Can't connect databasedbh->errstr\n";
}
##deal with argvs
if (@ARGV > 0)
{
if ($ARGV[0] eq "-c")
{
shift @ARGV;
@crashids = @ARGV;
}
else
{
@ids = @ARGV;
}
}
else
{
##get all numbers from database
my $statement = "SELECT Number,Type From Number";
my $sth = $dbh -> prepare($statement) or die "Can’t prepare $statement: $dbh->errstr\n";
$sth -> execute or die "can’t execute the query: $sth->errstr";
while (my @row = $sth -> fetchrow_array)
{
if ($row[1] == 1)
{
push @ids,$row[0];
}
else
{
push @crashids,$row[0];
}
}
}
foreach my $id(@ids)
{
##set source page
my $url = 'http://finance.sina.com.cn/fund/quotes/of'.$id.'/bc.shtml';
my $ua = LWP::UserAgent->new();
print "$id :\n";
$ua -> timeout(10);
##set proxy
$ua -> proxy('http', 'http://xxxxxxxxx:1080/');
my $r = $ua->get($url);
##encode source code,get useful data
if ($r -> is_success)
{
my $re = encode("utf8",$r -> decoded_content);
my $start = '<div id="relative_stocks_list"';
my $end = '</div>';
my $pr = "";
if ($re =~ /($start.*?$end)/s)
{
$pr = $1;
}
my %value;
my @dates;
$start = '<td>';
$end = '</span>';
while ($pr =~ /$start(\d{2}).*?:.*?(\d\.\d*)$end/gs)
{
##set date
my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime();
if ($1 <= $day)
{
$mon = $mon+1;
}
if ($mon < 10)
{
$mon = "0".$mon;
}
$year = $year+1900;
my $date = "$year-$mon-$1";
push @dates,$date;
$value{$date} = $2;
}
##write to database
for (@dates)
{
my $sql = "DELETE FROM Data where Date ='".$_."' and Number = '".$id."'";
$dbh -> do($sql);
$sql = 'INSERT INTO Data(Date,Number,Value) VALUES ("'.$_.'","'.$id.'","'.$value{$_}.'")';
$dbh -> do($sql);
print "$_ : $value{$_}\n";
}
}
print "-----------------------------\n";
}
foreach my $id(@crashids)
{
##set source page
my $url = 'http://finance.sina.com.cn/fund/quotes/of'.$id.'/bc.shtml';
my $ua = LWP::UserAgent->new();
print "$id :\n";
$ua -> timeout(10);
$ua -> proxy('http', 'http://xxxxxx:1080/');
my $r = $ua->get($url);
if ($r -> is_success)
{
my $re = encode("utf8",$r -> decoded_content);
my $start = '<div class="marketImg" id="moneyTypeTbl"';
my $end = '</div>';
my $pr = "";
if ($re =~ /$start.*?$end.*?$end(.*?$end)/s)
{
$pr = $1;
}
$start = '<tr><td>';
$end = '<tr>';
if ($pr =~/($start\d{4}.*$end)/s)
{
$pr = $1;
}
my %value;
my @dates;
$start = '<tr>';
$end = '</tr>';
while ($pr =~ /$start.*?(\d{4}.\d{2}.\d{2}).*?(\d\.\d*).*?(\d{1,2}\.\d*).*?$end/gs)
{
push @dates,$1;
$value{$1} = "$23";
}
##write to database
for (@dates)
{
my $sql = "DELETE FROM Data where Date ='".$_."' and Number = '".$id."'";
$dbh -> do($sql);
my ($val,$year) = split ":",$value{$_};
$sql = 'INSERT INTO Data(Date,Number,Value,Year) VALUES ("'.$_.'","'.$id.'","'.$val.'","'.$year.'")';
$dbh -> do($sql);
print "$_ : $value{$_}\n";
}
print "-----------------------------\n";
}
}
$dbh->disconnect;
exit;
分别采集所选基金的最近几天的净值,对于货币基金额外采集7日年化收益。打印出来同时存入数据库。
接下来所要做的就是分析数据,做出数据报表,画出净值日线图,与基准成本净值进行对比分析,同时根据自定义算法,进行成本核算,趋势分析。
to be continued...
原文地址 http://www.testwo.com/7225 |
|