【问题标题】:parsedhtml doesnt respond anymore已解析的html不再响应
【发布时间】:2018-05-15 07:21:12
【问题描述】:

所以我试图从网站上获取一些文本,一旦我尝试使用 ParsedHtml 返回一个对象,powershell 就会停止响应(即使我让它在后台运行几分钟,它也不会再做任何事情了)。 这可能是什么原因?

PS P:\> $url = "mywebsite"
PS P:\> $result = invoke-WebRequest $url
PS P:\> $result | Get-Member

TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject

Name              MemberType Definition
----              ---------- ----------
Dispose           Method     void Dispose(), void IDisposable.Dispose()
Equals            Method     bool Equals(System.Object obj)
GetHashCode       Method     int GetHashCode()
GetType           Method     type GetType()
ToString          Method     string ToString()
AllElements       Property               
Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;}
BaseResponse      Property   System.Net.WebResponse BaseResponse {get;set;}
Content           Property   string Content {get;}
Forms             Property           
Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;}
Headers           Property           
System.Collections.Generic.Dictionary[string,string] Headers {get;}
Images            Property   
Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}
InputFields       Property   
Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}
Links             Property       
Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}
ParsedHtml        Property   mshtml.IHTMLDocument2 ParsedHtml {get;}
RawContent        Property   string RawContent {get;set;}
RawContentLength  Property   long RawContentLength {get;}
RawContentStream  Property   System.IO.MemoryStream RawContentStream {get;}
Scripts           Property       
Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}
StatusCode        Property   int StatusCode {get;}
StatusDescription Property   string StatusDescription {get;}

PS P:\> $result.ParsedHtml | Get-Member

然后程序在最后一个命令后冻结。 弹出一个弹出窗口,询问我是否允许在我的电脑上保存 cookie,但单击是或否都无济于事。 这可能是什么原因?

$result.RawContent

例如可以正常工作并打印出所有 html 文本,但没有 getelementsby-Method,我猜它在 ParsedHtml 中,因此我需要它..它适用于例如 youtube,但适用于特定站点我想检查它是否冻结。 非常感谢任何帮助!

【问题讨论】:

  • 尝试在您的Invoke-WebRequest 通话中添加-UseBasicParsing 开关。
  • 刚试了下,$result 中已经没有 ParsedHtml 了。尝试访问 $result.ParsedHtml 时出现丢失对象错误,当我使用 $result | 时也不会出现不再使用 Get-Member 方法
  • 相信这是由 Internet Explorer 中的安全设置引起的。当它解析 HTML 时,它使用 IE,以及它的安全设置。
  • 我现在尝试解决。还有其他方法可以从网站上的表格中提取特定单词吗?
  • @btc 找到解决方案了吗?

标签: html powershell


【解决方案1】:

来自Invoke-WebRequest reference page on docs.microsoft.com

此参数已被弃用。从 PowerShell 6.0.0 开始, 所有 Web 请求仅使用基本解析。包含此参数 仅用于向后兼容,任何使用它都不会影响 cmdlet的操作。

以及来自 MS 工作人员评论的更详细解释 PowerShell Github repository Issue #2867:

Windows PowerShell 依靠 Internet Explorer 来解析 html。 由于 Internet Explorer 在我们支持的大多数平台上不可用 使用 PowerShell Core 6(纳米服务器、Linux、macOS), 默认为 -UseBasicParsing。 @MSAdministrator 的提议 ConvertFrom-Html 是一个更好的解决方案,而不是结合解析 Web cmdlet 的功能(如解析本地 html 文件)。 然后: 似乎社区已经通过 PowerShellGallery 上的模块来帮助填补这一空白,以专门处理解析 html。

今天似乎没有 ConvertFrom-Html,所以我猜你的选择是:提供解析的 PowerShell Gallery 模块,或者是有限的替代方案。看起来它们本身不会为您提供 ParsedHTML 属性,但它们确实为您提供了一些可能为您服务的可遍历/结构化内容:

https://stackoverflow.com/a/53878303/537243

在非常非常有限的情况下,您可以尝试使用“html 是 xml 的子类型”的方式,但是 xml 解析器会因为 html 中允许的大量语法“偏差”而感到困惑和失败,所以来源必须非常常规且非常普通:

$webresponse = Invoke-WebRequest -Uri "https://w3.org"
$xmldoc = [xml]$webresponse.Content
write-output $xmldoc.html.body.div[0].div.h1.span |select '#text'

【讨论】:

    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多