[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 523066680 于 2019-1-9 22:16 编辑

写过抓双色球往期结果的,多线程,以前写的丑(发的代码不完整,好像还有另一个脚本负责导出EXCEL,一时没找着)
  1. use Encode;
  2. use Modern::Perl;
  3. use Time::HiRes qw/time sleep/;
  4. use threads;
  5. use threads::shared;
  6. use Try::Tiny;
  7. use Mojo::UserAgent;
  8. use File::Basename;
  9. use File::Path qw/make_path/;
  10. use File::Slurp;
  11. STDOUT->autoflush(1);
  12. our $workdir = "D:\\Temp\\Double_Ball_Lottery";
  13. make_path $workdir unless -e $workdir;
  14. #chdir $workdir;
  15. our $ua;
  16. our $main = "http://kaijiang.500.com";
  17. our @links :shared;
  18. our @ths;
  19. $ua = Mojo::UserAgent->new();
  20. $ua = $ua->max_redirects(5);
  21. print "Getting Links ... ";
  22. get_links( \@links );
  23. say "Done";
  24. #创建线程
  25. grep { push @ths, threads->create( \&thread_func, $_ ) } ( 0 .. 3 );
  26. #等待运行结束
  27. while ( threads->list(threads::running) ) { sleep 0.2 };
  28. #线程分离/结束
  29. grep { $_->detach() } threads->list(threads::all);
  30. quit();
  31. sub thread_func
  32. {
  33.     our (@links, $workdir);
  34.     my ( $id ) = @_;
  35.     my $ua = Mojo::UserAgent->new();
  36.     my ($link, $file, $res, $times);
  37.     while ( $#links > 0 )
  38.     {
  39.         $link = shift @links;
  40.         $file = $workdir ."\\". basename($link);
  41.         
  42.         if ( -e $file ) { say "$id - $link already exists"; next };
  43.         say "$id - $link";
  44.         $times = 0;
  45.         while (1)
  46.         {
  47.             try { $res = $ua->get($link)->result  }
  48.             catch
  49.             {
  50.                 printf "[%d] getting %s, retry: %d\n", $id, basename($link), $times++;
  51.                 sleep 3.0;
  52.             };
  53.             last if ( defined $res and $res->is_success );
  54.             return if ( $times > 10 );
  55.         }
  56.         write_file( $workdir ."\\". basename($link), $res->body );
  57.     }
  58. }
  59. sub get_links
  60. {
  61.     my ($aref) = @_;
  62.     my $html = read_file("simple.htm");
  63.     my $dom = Mojo::DOM->new( $html );
  64.     for my $e ( $dom->at(".iSelectList")->find("a")->each )
  65.     {
  66.         push @$aref, $e->attr("href");
  67.     }
  68. }
  69. sub quit { system("pause"); }
复制代码
原来是18年2月写的,之前抓了03年到18年1月的html,导出excel(没显示日期,就是搞个分布图),
https://share.weiyun.com/5V1MvmE

TOP

返回列表