powershell網(wǎng)絡(luò)蜘蛛解決亂碼問(wèn)題
抓?。ㄅ廊。┚W(wǎng)上信息的腳本程序,俗稱(chēng)網(wǎng)絡(luò)蜘蛛。
powershell中自帶了這樣的兩個(gè)命令,【Invoke-WebRequest】和【Invoke-RestMethod】,但這兩個(gè)命令有時(shí)候會(huì)亂碼。
現(xiàn)在轉(zhuǎn)帖分享, 某個(gè)【歪果仁】寫(xiě)的腳本。來(lái)源于 墻外出處: https://gist.github.com/angel-vladov/9482676
核心代碼
function Read-HtmlPage { param ([Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][String] $Uri) # Invoke-WebRequest and Invoke-RestMethod can't work properly with UTF-8 Response so we need to do things this way. [Net.HttpWebRequest]$WebRequest = [Net.WebRequest]::Create($Uri) [Net.HttpWebResponse]$WebResponse = $WebRequest.GetResponse() $Reader = New-Object IO.StreamReader($WebResponse.GetResponseStream()) $Response = $Reader.ReadToEnd() $Reader.Close() # Create the document class [mshtml.HTMLDocumentClass] $Doc = New-Object -com "HTMLFILE" $Doc.IHTMLDocument2_write($Response) # Returns a HTMLDocumentClass instance just like Invoke-WebRequest ParsedHtml $Doc #powershell 傳教士 轉(zhuǎn)帖并修改的文章 2016-01-01, 允許再次轉(zhuǎn)載,但必須保留名字和出處,否則追究法律責(zé)任 }
原文函數(shù)
function Read-HtmlPage { param ([Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][String] $Uri) # Invoke-WebRequest and Invoke-RestMethod can't work properly with UTF-8 Response so we need to do things this way. [Net.HttpWebRequest]$WebRequest = [Net.WebRequest]::Create($Uri) [Net.HttpWebResponse]$WebResponse = $WebRequest.GetResponse() $Reader = New-Object IO.StreamReader($WebResponse.GetResponseStream()) $Response = $Reader.ReadToEnd() $Reader.Close() # Create the document class [mshtml.HTMLDocumentClass] $Doc = New-Object -com "HTMLFILE" $Doc.IHTMLDocument2_write($Response) # Returns a HTMLDocumentClass instance just like Invoke-WebRequest ParsedHtml $Doc }
PowerShell function you can use for reading UTF8 encoded HTML pages content. The built in Invoke-WebRequest and Invoke-RestMethod fail miserably.
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。