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 whatever
close $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
标签:use,同花顺,title,--,ua,yjyg,perl,print,my From: https://blog.csdn.net/belldeep/article/details/136969875