powershell網(wǎng)絡(luò)蜘蛛解決亂碼問題
抓?。ㄅ廊。┚W(wǎng)上信息的腳本程序,俗稱網(wǎng)絡(luò)蜘蛛。
powershell中自帶了這樣的兩個(gè)命令,【Invoke-WebRequest】和【Invoke-RestMethod】,但這兩個(gè)命令有時(shí)候會亂碼。
現(xiàn)在轉(zhuǎn)帖分享, 某個(gè)【歪果仁】寫的腳本。來源于 墻外出處: 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.
相關(guān)文章
PowerShell中調(diào)用.NET對象的靜態(tài)方法、靜態(tài)屬性和類方法、類屬性例子
這篇文章主要介紹了PowerShell中調(diào)用.NET對象的靜態(tài)方法、靜態(tài)屬性和類方法、類屬性例子,即PowerShell中如何使用.NET對象的一些方法,需要的朋友可以參考下2014-08-08PowerShell Out-File向只讀文件寫入內(nèi)容的方法
這篇文章主要介紹了PowerShell Out-File向只讀文件寫入內(nèi)容的方法,只需要加一個(gè)-Force參數(shù)即可,需要的朋友可以參考下2014-08-08PowerShell腳本實(shí)現(xiàn)網(wǎng)卡DHCP自動獲取IP地址、設(shè)置靜態(tài)IP地址的方法
這篇文章主要介紹了PowerShell腳本實(shí)現(xiàn)網(wǎng)卡DHCP自動獲取IP地址、設(shè)置靜態(tài)IP地址的方法,本文同時(shí)講解了用PowerShell設(shè)置網(wǎng)卡DHCP、靜態(tài)IP的方法,需要的朋友可以參考下2014-08-08PowerShell使用Clear-Content命令刪除、清空文件內(nèi)容的例子
這篇文章主要介紹了PowerShell使用Clear-Content命令刪除、清空文件內(nèi)容的例子,并對Clear-Content作了介紹,需要的朋友可以參考下2014-08-08PowerShell使用枚舉變量定義帶智能提示功能的函數(shù)參數(shù)
這篇文章主要介紹了PowerShell使用枚舉變量定義帶智能提示功能的函數(shù)參數(shù),但定義后只在ISE當(dāng)中有效,需要的朋友可以參考下2014-07-07你應(yīng)該選擇 Powershell 的10個(gè)理由(拋棄 cmd)
Windows平臺的開發(fā)者們,是時(shí)候拋棄 cmd 了,這篇文章主要介紹了你應(yīng)該選擇 Powershell 的10個(gè)理由,需要的朋友可以參考下2017-10-10