本帖最后由 flashercs 于 2019-9-3 19:46 编辑
- <#*,:&cls
- @echo off
- pushd "%~dp0"
- Powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- $VerbosePreference = "Continue"
- $inputFile = "单词列表.txt"
- $outputFile = "结果.txt"
- Get-Content -LiteralPath $inputFile -OutBuffer 10 | ForEach-Object -Begin {
- $sw = New-Object -TypeName System.IO.StreamWriter -ArgumentList ("$pwd\$outputFile"), $false, ([System.Text.Encoding]::Default)
- # webclient settings
- $webclient = New-Object -TypeName System.Net.WebClient
- $webclient.BaseAddress = 'http://dict.cn/'
- $webclient.Encoding = [System.Text.Encoding]::UTF8
- $webclient.Headers.Add("Accept", "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8")
- $webclient.Headers.Add("Accept-Encoding", "gzip")
- $webclient.Headers.Add("Accept-Language", "en-US, en; q=0.8, zh-Hans-CN; q=0.5, zh-Hans; q=0.3")
- # xml parser
- $xmldoc = New-Object -TypeName System.Xml.XmlDocument
- # re
- $recontent = [regex]'(?si)<div\s+class="word"[^>]*>.*?(?=<div\s+class="section[^"]*"[^>]*>)'
- $rejs = [regex]'(?si)<script[^>]*>.*?</script>'
- # stringbuilder
- $strbuilder = New-Object -TypeName System.Text.StringBuilder
- Add-Type -AssemblyName Microsoft.Jscript
- $vsaengine = [Microsoft.JScript.Vsa.VsaEngine]::CreateEngine()
- Add-Type -AssemblyName System.Web
- } -Process {
- Write-Verbose "Fetching $_ ..."
- for ($i = 2; $i -ge 0; $i--) {
- try {
- $readstream = $webclient.OpenRead($_)
- Write-Verbose "Fetch $_ success"
- break
- }
- catch {
- $_ | Out-String | Write-Host -ForegroundColor Red
- }
- }
- if ($readstream) {
- try {
- $gzipstream = New-Object -TypeName System.IO.Compression.GZipStream -ArgumentList $readstream, ([System.IO.Compression.CompressionMode]::Decompress)
- $sr = New-Object -TypeName System.IO.StreamReader -ArgumentList $gzipstream, ([System.Text.Encoding]::UTF8)
-
- $match = $recontent.Match($sr.ReadToEnd())
- if ($match.Success) {
- Write-Verbose "Match $_ success"
- $xmldoc.LoadXml(($rejs.Replace($match.Value, '') -replace '<([^\x00-\x7e]+)>', '<$1>'))
- $strbuilder.Length = 0
- # word-cont
- [void]$strbuilder.Append($xmldoc.SelectSingleNode('//h1[@class="keyword"]/text()').Value).Append("`t")
- # dict-translation
- [void]$strbuilder.Append( ($xmldoc.SelectNodes('//ul/li[position()<last()]') | ForEach-Object { $_.innerText }) -join " " ).Append("`t")
- try {
- # dict-chart
- $strjson = [System.Web.HttpUtility]::UrlDecode($xmldoc.SelectSingleNode('//div[@id="dict-chart-basic"]/@data').Value)
- $jsobj = [Microsoft.JScript.Eval]::JScriptEvaluate("($strjson)", $vsaengine )
- foreach ($field in $jsobj) {
- [void]$strbuilder.Append($jsobj.Item($field).Item('sense')).Append(':').Append($jsobj.Item($field).Item('percent')).Append(',')
- }
- [void]$strbuilder.Remove($strbuilder.Length - 1, 1)
- }
- catch {
- # $_ | Out-String | Write-Host -ForegroundColor Red
- }
- # output result string
- $sw.WriteLine(($strbuilder.ToString() -replace "[\r\n]"))
- # $strbuilder.ToString()|Out-Host
- }
- else {
- Write-Verbose "Match $_ failed"
- }
- }
- catch {
- $_ | Out-String | Write-Host -ForegroundColor Red
- }
- finally {
- $readstream.Close()
- Remove-Variable readstream
- if ($gzipstream) {
- $gzipstream.Dispose()
- }
- if ($sr) {
- $sr.Dispose()
- }
- }
- }
- else {
- $sw.WriteLine($_)
- }
- } -End {
- $sw.Dispose()
- $webclient.Dispose()
- }
复制代码
|