[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
代码存为bat,win10/11系统运行,爬取太快会限制
  1. <# :
  2. cls&echo off&cd /d "%~dp0"&rem bat存为ANSI/GB2312编码
  3. path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%path%
  4. set "current=%cd%"
  5. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
  6. pause
  7. exit
  8. #>
  9. $current=($env:current).trimend('\');
  10. $rooturl='https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/index.html';
  11. $outfile=$current+'\结果.csv';
  12. $pyfile=$current+'\py.txt';
  13. if(-not (test-path -literal $pyfile)){
  14.     Invoke-WebRequest -Uri 'https://csstools.chinaz.com/tools/js/pinyin.js' -OutFile $pyfile;
  15. }
  16. $enc=[System.Text.Encoding]::UTF8;
  17. $text=[IO.File]::ReadAllText($pyfile, $enc);
  18. $pydic=New-Object 'System.Collections.Generic.Dictionary[string, string]';
  19. $m=[regex]::match($text, 'var pydic ?= ?"([^"]+)"');
  20. if($m.success){
  21.     $arr=$m.groups[1].value.trimend(',').split(',');
  22.     for($i=0;$i -lt $arr.length;$i++){
  23.         $han=$arr[$i].Substring(0,1);
  24.         $py=((((($arr[$i].Substring(1) -replace '[āáǎà]','a') -replace '[ōóǒò]','o') -replace '[ēéěè]','e') -replace '[īíǐì]','i') -replace '[ūúǔù]','u') -replace '[üǖǘǚǜ]','v';
  25.         if($py.length -le 1){$py=$py.toUpper();}else{$py=$py.Substring(0,1).toUpper()+$py.Substring(1);}
  26.         $pydic.add($han, $py);
  27.     }
  28. }
  29. $fs=New-Object System.IO.FileStream($outfile, [System.IO.FileMode]::Create);
  30. $sw=New-Object System.IO.StreamWriter($fs, [Text.Encoding]::GetEncoding('GB2312'));
  31. function hantopy($h, $s){
  32.     $brr=New-Object -TypeName System.Collections.ArrayList;
  33.     for($i=0;$i -lt $h.length;$i++){
  34.         $char=$h.Substring($i, 1);
  35.         if($pydic.ContainsKey($char)){
  36.             if($s -eq 1){
  37.                 [void]$brr.add($pydic[$char]);
  38.             }else{
  39.                 [void]$brr.add($pydic[$char].Substring(0, 1));
  40.             }
  41.         }else{
  42.             [void]$brr.add($char);
  43.         }
  44.     }
  45.     return ($brr -join '');
  46. }
  47. function gethtml($u){
  48.     $t='';
  49.     $ua='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0';
  50.     for($j=1;$j -le 10;$j++){
  51.         try{
  52.             $req=Invoke-WebRequest -UseBasicParsing -Uri $u -TimeoutSec 8 -UserAgent $ua;
  53.             if($req.StatusCode -eq 200){
  54.                 [byte[]] $bytes=[Text.Encoding]::GetEncoding('ISO-8859-1').GetBytes($req.Content);
  55.                 $t=[Text.Encoding]::GetEncoding('UTF-8').GetString($bytes)
  56.                 break;
  57.             }
  58.         }catch{
  59.             write-host ('第'+$j.ToString()+'次获取网页内容失败');
  60.             start-sleep -Seconds 10;
  61.         }
  62.     }
  63.     start-sleep -Seconds 5;
  64.     return $t;
  65. }
  66. function geturl($html, $url){
  67.     $from=$url -replace '[^//]+$','';
  68.     $m1=[regex]::matches($html, '<tr class="(provincetr|citytr|countytr|towntr|villagetr)"[^>]*?>([\s\S]+?)</tr>');
  69.     if($m1.count -ge 1){
  70.         for($i=0;$i -lt $m1.count;$i++){
  71.             $m2=[regex]::matches($m1[$i].groups[2].value, '<td(?: [^>]+?)?>(.+?)</td>');
  72.             if($m2.count -ge 1){
  73.                 if($m1[$i].groups[1].value -eq 'provincetr'){
  74.                     for($j=0;$j -lt $m2.count;$j++){
  75.                         $item=@{
  76.                             'code1'='';
  77.                             'code2'='';
  78.                             'url'='';
  79.                             'name'='';
  80.                             'pinyin1'='';
  81.                             'pinyin2'=''
  82.                         };
  83.                         $m3=[regex]::match($m2[$j].groups[1].value, '<a href="([^"]+?)"[^>]*?>([\s\S]+?)</a>');
  84.                         if($m3.Success){
  85.                             $item['url']=$from+$m3.groups[1].value;
  86.                             $item['name']=($m3.groups[2].value -replace '<[^>]+?>','').trim();
  87.                             $item['pinyin1']=hantopy $item['name'] 1;
  88.                             $item['pinyin2']=hantopy $item['name'] 2;
  89.                         }
  90.                         write-host $item['name'];
  91.                         $line="'"+$item['code1']+','+$item['name']+','+$item['pinyin1']+','+$item['pinyin2'];
  92.                         $sw.WriteLine($line);
  93.                         $sw.Flush();
  94.                         if($item['url'] -ne ''){
  95.                             geturl (gethtml $item['url']) $item['url'];
  96.                         }
  97.                     }
  98.                 }else{
  99.                     $item=@{
  100.                         'code1'='';
  101.                         'code2'='';
  102.                         'url'='';
  103.                         'name'='';
  104.                         'pinyin1'='';
  105.                         'pinyin2'=''
  106.                     };
  107.                     if($m1[$i].groups[1].value -eq 'villagetr'){
  108.                         $item['code1']=($m2[0].groups[1].value -replace '<[^>]+?>','').trim();
  109.                         $item['code2']=($m2[1].groups[1].value -replace '<[^>]+?>','').trim();
  110.                         $item['name']=($m2[2].groups[1].value -replace '<[^>]+?>','').trim();
  111.                     }else{
  112.                         $item['code1']=($m2[0].groups[1].value -replace '<[^>]+?>','').trim();
  113.                         $item['name']=($m2[1].groups[1].value -replace '<[^>]+?>','').trim();
  114.                         $m3=[regex]::match($m2[1].groups[1].value, '<a href="([^"]+?)"[^>]*?>([\s\S]+?)</a>');
  115.                         if($m3.Success){
  116.                             $item['url']=$from+$m3.groups[1].value;
  117.                         }
  118.                     }
  119.                     $item['pinyin1']=hantopy $item['name'] 1;
  120.                     $item['pinyin2']=hantopy $item['name'] 2;
  121.                     write-host $item['name'];
  122.                     $line="'"+$item['code1']+','+$item['name']+','+$item['pinyin1']+','+$item['pinyin2'];
  123.                     $sw.WriteLine($line);
  124.                     $sw.Flush();
  125.                     if($item['url'] -ne ''){
  126.                         if($m1[$i].groups[1].value -notmatch '(towntr|villagetr)'){
  127.                             geturl (gethtml $item['url']) $item['url'];
  128.                         }
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.     }
  134. }
  135. #$rooturl='https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/11/01/110102.html'
  136. geturl (gethtml $rooturl) $rooturl;
  137. $sw.Close();
  138. $fs.Close();
  139. exit;
复制代码
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

回复 4# 曾经的你

链接: https://pan.baidu.com/s/147J8ycl9FQJhYPU9-YxHjw?pwd=g84x
我测试一切正常,你说的问题都没出现,而且你这样反馈问题毫无意义,没有截图,没有贴出报错信息,也没有标明或贴出你的修改
另外请按有偿发帖规则贴出你的及时联系方式,或者按回答者提供的联系方式去及时反馈,不然你这样只会浪费别人的时间,让人失去热情
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

返回列表