powershell网络蜘蛛解决乱码问题

 更新时间:2017年10月01日 20:07:17   作者:传教士  
这篇文章主要介绍了powershell网络蜘蛛解决乱码问题,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

抓取(爬取)网上信息的脚本程序,俗称网络蜘蛛。
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小技巧之使用Jint引擎在PowerShell中执行Javascript函数

    Powershell小技巧之使用Jint引擎在PowerShell中执行Javascript函数

    这里演示如何利用PowerShell将一段Javascript函数字符串交给Jint引擎去执行。方法很简单,希望对大家有所帮助
    2014-09-09
  • PowerShell小技巧之同时使用可选强制参数

    PowerShell小技巧之同时使用可选强制参数

    本文主要讲诉了在脚本函数中让可选参数和强制参数必须同时使用,有需要的朋友可以参考下。
    2014-09-09
  • powershell操作word详解

    powershell操作word详解

    这篇文章主要介绍了powershell操作word示例,需要的朋友可以参考下
    2014-05-05
  • PowerShell小技巧之查找获取注册表路径

    PowerShell小技巧之查找获取注册表路径

    这篇文章主要介绍了在PowerShell中使用递归查找获取注册表路径的小技巧,有需要的朋友可以参考下
    2014-09-09
  • PowerShell获取当前进程PID的小技巧

    PowerShell获取当前进程PID的小技巧

    这篇文章主要介绍了PowerShell获取当前进程PID的小技巧,直接使用一个全局变量$pid即可获取当前进程的PID,需要的朋友可以参考下
    2014-08-08
  • PowerShell命令中包含空格如何运行?

    PowerShell命令中包含空格如何运行?

    这篇文章主要介绍了PowerShell命令中包含空格如何运行?即在PowerShell中运行包含空格的命令的方法,需要的朋友可以参考下
    2014-08-08
  • 使用PowerShell获取Trustedinstaller权限的问题

    使用PowerShell获取Trustedinstaller权限的问题

    这篇文章主要介绍了使用PowerShell获取Trustedinstaller权限,获取到?Trustedinstaller?权限,就可以通过一些命令来修改系统文件了,本文给大家详细讲解,需要的朋友可以参考下
    2023-01-01
  • Windows Powershell调用静态方法

    Windows Powershell调用静态方法

    Powershell将信息存储在对象中,每个对象都会有一个具体的类型,简单的文本会以System.String类型存储,日期会以System.DateTime类型存储。任何.NET对象都可以通过GetType()方法返回它的类型,该类型中有一个FullName属性,可以查看类型的完整名称。
    2014-09-09
  • Shell?中常用?Date?日期的计算

    Shell?中常用?Date?日期的计算

    这篇文章主要为大家介绍了Shell中常用Date日期的计算详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • 用PowerShell代替批处理吧!

    用PowerShell代替批处理吧!

    这篇文章主要介绍了用PowerShell代替批处理吧!本文讲解了批处理文件的历史、Windows NT 和 Cmd.exe、Windows Script 主机、进入 Windows PowerShell、为什么是时候停止写批处理了等内容,需要的朋友可以参考下
    2015-03-03

最新评论

?


http://www.vxiaotou.com