powershell網(wǎng)絡(luò)蜘蛛解決亂碼問題
抓?。ㄅ廊。┚W(wǎng)上信息的腳本程序,俗稱網(wǎng)絡(luò)蜘蛛。
powershell中自帶了這樣的兩個(gè)命令,【Invoke-WebRequest】和【Invoke-RestMethod】,但這兩個(gè)命令有時(shí)候會(huì)亂碼。
現(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小技巧之True和False的類型轉(zhuǎn)換
這篇文章主要介紹了在PowerShell中將True和False的類型互相轉(zhuǎn)換的幾種方法,非常簡(jiǎn)單實(shí)用,有需要的朋友參考下2014-09-09
使用PowerShell將Excel工作表另存為獨(dú)立文件
這篇文章主要為大家介紹了如何使用?PowerShell?腳本將一個(gè)?Excel?文件中的每個(gè)工作表單獨(dú)另存為獨(dú)立的?Excel?文件,以提高工作效率,需要的可以參考一下2023-08-08
PowerShell函數(shù)參數(shù)用星號(hào)隱藏的方法
這篇文章主要介紹了PowerShell函數(shù)參數(shù)用星號(hào)隱藏的方法,這是一個(gè)很實(shí)用的技巧,比如在一些需要輸入密碼的情況,需要的朋友可以參考下2014-07-07
探索PowerShell (二) PowerShell的基本操作
這里介紹下如何打開powershell控制臺(tái),在 程序>附件>windows powershell中即可,主要是界面不再是dos窗口,據(jù)說功能也增加了很多2012-12-12

