您现在的位置是:网站首页> 编程资料编程资料
powershell网络蜘蛛解决乱码问题_PowerShell_
2023-05-26
379人已围观
简介 powershell网络蜘蛛解决乱码问题_PowerShell_
抓取(爬取)网上信息的脚本程序,俗称网络蜘蛛。
powershell中自带了这样的两个命令,【Invoke-WebRequest】和【Invoke-RestMethod】,但这两个命令有时候会乱码。
现在转帖分享, 某个【歪果仁】写的脚本。来源于 墙外出处: 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 传教士 转帖并修改的文章 2016-01-01, 允许再次转载,但必须保留名字和出处,否则追究法律责任 }原文函数
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.
相关内容
- 你应该选择 Powershell 的10个理由(抛弃 cmd)_PowerShell_
- PowerShell中使用curl(Invoke-WebRequest)的方法教程_PowerShell_
- 详谈Ubuntu PowerShell(小白入门必看教程)_PowerShell_
- PowerShell管道入门必看篇(管道例子大全)_PowerShell_
- PowerShell使用match操作符来筛选数组_PowerShell_
- PowerShell连接SQL SERVER数据库进行操作的实现代码_PowerShell_
- PowerShell 读取性能计数器二进制文件(.blg)记录并汇总计算_PowerShell_
- powershell远程管理服务器磁盘空间的实现代码_PowerShell_
- Powershell 之批量获取文件大小的实现代码_PowerShell_
- 用PowerShell删除N天前或指定日期(前后)创建(或修改)的文件_PowerShell_
