powershell网络蜘蛛解决乱码问题
更新时间:2017年10月01日 20:07:17 作者:传教士
这篇文章主要介绍了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.
相关文章
-
本文主要记录了本人安装office2013后遇到的一个小需求,然后用powershell实现了这个功能,特此记录下,并附上copy-item的使用方法2014-09-09
-
这篇文章主要介绍了PowerShell入门教程之PowerShell有什么用?本文讲解了PowerShell的三大作用,需要的朋友可以参考下2014-10-10
-
在Windows Serve 2008 R2中,一个重要改进就是PowerShell版本升级为2.0。Win 2008 R2包括一系列新的服务器管理界面,这些均建立在PowerShell 2.0之上。它新增了240个cmdlets命令集,新的PowerShell图形用户界面也增添了开发功能,从而用户能更简单创建自己的命令行。2015-09-09
-
这篇文章主要介绍了Windows Powershell 快捷键介绍,需要的朋友可以参考下2014-08-08
-
这篇文章主要介绍了PowerShell中终止管道的方法,本文直接给出代码实例,需要的朋友可以参考下2015-05-05
-
Do和While可能产生死循环,为了防止死循环的发生,你必须确切的指定循环终止的条件。指定了循环终止的条件后,一旦条件不满足就会退出循环。2014-10-10
-
Powershell直接脚本时出现无法加载文件因为在此系统中禁止执行脚本,有关此问题的解决方法如下2014-08-08
-
这篇文章主要介绍了powershell远程管理服务器磁盘空间的实现代码,需要的朋友可以参考下2016-11-11
-
这篇文章主要介绍了PowerShell中获取Windows系统序列号的脚本分享,本文方法是读取注册表中的信息,然后处理成序列号输出,需要的朋友可以参考下2014-11-11
-
这篇文章主要介绍了Powershell实现捕获系统内置EXE程序的异常,系统内置的EXE程序是指如robocopy.exe、ipconfig.exe等命令的实现程序,需要的朋友可以参考下2014-12-12
最新评论