| @powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" &pause&exit |
| |
| $unicodeFile="C:\Users\lenovo\Desktop\a.txt"; |
| $saveFile="C:\Users\lenovo\Desktop\data.csv"; |
| $maxThreadCount=5; |
| if((Test-Path $saveFile)){Remove-Item -Path $saveFile} |
| |
| $queryJob={ |
| param($unicode="") |
| if($unicode -eq ""){return;} |
| $word="";$kMandarin="" |
| $ie=New-Object -ComObject "InternetExplorer.Application"; |
| $ie.Visible=$false; |
| $ie.Navigate("https://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint="+$unicode); |
| while($ie.Busy){Start-Sleep -Seconds 1} |
| $text=$ie.Document.querySelector("body").innerText; |
| if($text -match "Your Browser\s*(\S*?)\s"){$word=$Matches[1]} |
| if($text -match "kMandarin\s*(\S*?)\s"){$kMandarin=$Matches[1]} |
| $ie.Quit(); |
| return $unicode+","+$word+","+$kMandarin; |
| } |
| |
| $pool=[runspacefactory]::CreateRunspacePool(1,$maxThreadCount); |
| $pool.Open(); |
| $threads=New-Object "System.Collections.ArrayList"; |
| $results=New-Object "System.Collections.ArrayList"; |
| |
| Get-Content $unicodeFile | Select-Object -Unique | foreach { |
| Write-Host ("添加任务:{0}" -f $_); |
| $thread=[powershell]::Create(); |
| $thread.RunspacePool=$pool; |
| [void]$thread.AddScript($queryJob); |
| [void]$thread.AddArgument($_.Trim()); |
| [void]$threads.Add($thread); |
| [void]$results.Add($thread.BeginInvoke()); |
| } |
| Write-Host "任务全部创建完成!`n" |
| |
| $datas=New-Object "System.Collections.ArrayList"; |
| $count=0; |
| while($true){ |
| $allDone=$true; |
| for($i=0;$i -lt $results.Count;$i++){ |
| if($results[$i] -ne $null){ |
| if($results[$i].IsCompleted){ |
| $data=$threads[$i].EndInvoke($results[$i])[0]; |
| Write-Host ("{0} 任务完成" -f $data); |
| [void]$datas.Add($data); |
| $threads[$i].Dispose(); |
| $threads[$i]=$null; |
| $results[$i]=$null; |
| $count++; |
| [System.Console]::Title="进度:{0}/{1} " -f $count,$threads.Count; |
| } else { |
| $allDone=$false; |
| } |
| } |
| } |
| if($allDone){break}; |
| Start-Sleep -Seconds 1 |
| } |
| Write-Host "任务全部完成!`n" |
| |
| $datas | Out-String | Out-File $saveFile -Encoding unicode |
| Write-Host ("已写入文件: {0}" -f $saveFile); |
| |
| $pool.Close(); COPY |