本文主要是介绍perl:获取同花顺数据--业绩预告,送股派息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
perldoc LWP::UserAgent
如果没有安装,则安装模块,运行 cpanm LWP::UserAgent 。
编写 get_yjyg_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/yjyg/');if ($response->is_success) {print $DATA $response->decoded_content; # or whateverclose $DATA;$tree->parse_file("data.html");
} else {print " get data failed.";exit;
}my $title= $tree->findvalue('/html/body//span[@class="text-value"]');
print " title is $title\n";
$title ='yjyg-2024Q1';
my $ps = 1;
print " pages is $ps\n";sub utf2gbk { encode('gbk', decode('utf8', $_[0]));} for (my $n=1; $n<=$ps; $n++){open(my $T, ">", "$title-$n.csv");my @arr1= $tree->find_by_tag_name("tr") ;#shift @arr1;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;
}
运行 chcp 65001
perl get_yjyg_10jqka.pl
生成 yjyg-2024Q1-1.csv
编写 get_sgpx_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,'>', "data3.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/sgpx/');if ($response->is_success) {
# print $DATA $response->decoded_content; # or whatever
# close $DATA;$tree->parse_file("data3.html");
} else {print " get data failed.";exit;
}my $title= $tree->findvalue('/html/body//span[@class="text-value"]');
print " title is $title\n";
$title ='sgpx-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]));} for (my $n=1; $n<=1; $n++){open(my $T, ">", "$title-$n.csv");my @arr1= $tree->find_by_tag_name("tr") ;shift @arr1;foreach my $row (@arr1) {my @arr2= $row->content_list;my @str;foreach my $a (@arr2){ push @str, $a->as_text;}my $line = join(',', @str); print $T "$line\n"; }close $T;
}
运行 perl get_sgpx_10jqka.pl
title is 2023年年报
sgpx-2023Y pages is 6
生成 sgpx-2023Y-1.csv
这篇关于perl:获取同花顺数据--业绩预告,送股派息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!