本文主要是介绍perl:获取同花顺数据--业绩快报,业绩公告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
perldoc LWP::UserAgent
如果没有安装,则安装模块,运行 cpanm LWP::UserAgent 。
编写 get_yjkb_10jqka.pl 如下
#!/usr/bin/perl
# perl 获取同花顺数据--业绩快报
use LWP::UserAgent;
use Encode qw(decode encode);
use POSIX;
use Data::Dumper;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;my $tree= HTML::TreeBuilder::XPath->new;
open(my $DATA,'>', "data.html") or die "open data file failed:$!"; my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $response = $ua->get('http://data.10jqka.com.cn/financial/yjkb/');if ($response->is_success) {print $DATA $response->decoded_content; # or whatever
# print $response->decoded_content; # or whatever$tree->parse_file( "data.html");
};my $title= $tree->findvalue('/html/body//span[@class="text-value"]');
print " title is $title\n";
$title ='yjkb-2023Y';
my $pages = $tree->findvalue('/html/body//span[@class="page_info"]');
my @ps = split('/', $pages);
print " $title pages is $ps[1]\n";sub utf2gbk { encode('gbk', decode('utf8', $_[0]));}
# 表头一行
my $headline ="序号,股票代码,股票简称,公告日期,营业收入(元),去年同期(元),同比增长(%),季度环比增长(%),净利润(元),去年同期(元),同比增长(%),季度环比增长(%),每股收益(元),每股净资产(元),净资产收益率(%),更多\n";for (my $n=1; $n<=1; $n++){open(my $T, ">", "$title-$n.csv");print $T utf2gbk($headline);my @arr1= $tree->find_by_tag_name("tr") ;shift @arr1; shift @arr1; # 忽略表头2行foreach my $row (@arr1) {my @arr2= $row->content_list;my @str;foreach my $a (@arr2){ push @str, $a->as_text;}my $line = utf2gbk(join(',', @str)); print $T "$line\n"; }close $T;
}
注意:源程序 编码:UTF-8.
运行 cmd
chcp 65001
perl get_yjkb_10jqka.pl
title is 2023年年报
yjkb-2023Y pages is 23
生成 yjkb-2023Y-1.csv
编写 get_yjgg_10jqka.pl 如下
#!/usr/bin/perl
# perl 获取同花顺数据--业绩公告
use LWP::UserAgent;
use Encode qw(decode encode);
use POSIX;
use Data::Dumper;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;my $tree= HTML::TreeBuilder::XPath->new;
open(my $DATA,'>', "data.html") or die "open data file failed:$!"; my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $response = $ua->get('http://data.10jqka.com.cn/financial/yjgg/');if ($response->is_success) {print $DATA $response->decoded_content; # or whatever
# print $response->decoded_content; # or whatever$tree->parse_file( "data.html");
};my $title= $tree->findvalue('/html/body//span[@class="text-value"]');
print " title is $title\n";
$title ='yjgg-2023Y';
my $pages = $tree->findvalue('/html/body//span[@class="page_info"]');
my @ps = split('/', $pages);
print " $title pages is $ps[1]\n";sub utf2gbk { encode('gbk', decode('utf8', $_[0]));} # 表头一行
my $headline ="序号,股票代码,股票简称,公告日期,营业收入(元),去年同期(元),同比增长(%),季度环比增长(%),净利润(元),去年同期(元),同比增长(%),季度环比增长(%),每股收益(元),每股净资产(元),净资产收益率(%),更多\n";for (my $n=1; $n<=1; $n++){open(my $T, ">", "$title-$n.csv");print $T utf2gbk($headline);my @arr1= $tree->find_by_tag_name("tr") ;shift @arr1; shift @arr1; # 忽略表头2行foreach my $row (@arr1) {my @arr2= $row->content_list;my @str;foreach my $a (@arr2){ push @str, $a->as_text;}my $line = utf2gbk(join(',', @str)); print $T "$line\n"; }close $T;
}
运行 perl get_yjgg_10jqka.pl
title is 2023年年报
yjgg-2023Y pages is 7
生成 yjgg-2023Y-1.csv
这篇关于perl:获取同花顺数据--业绩快报,业绩公告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!