如何使用 -UseBasicParsing 在 Powershell 5 中解析 HTML
How to Parse HTML in Powershell 5 with -UseBasicParsing
通常 Invoke-WebRequest
更适合与 -UseBasicParsing
参数一起使用,如果不使用 Invoke-RestMethod
命令以提高性能和网络节省。
但是这些结果没有好的 ol'PARSEHTML
方法。
我们如何使用规定的命令设置解析 html?
Invoke-Webrequest $site -UseBasicParsing
Invoke-RestMethod $site
可以通过创建一个新的 HTML 对象并写入其 IHTMLDocument2 部分
来解决该场景
注意:这仅在 WINDOWS POWERSHELL 5.0 和 5.1
中有效
您可以按照以下方式处理列出的场景:
对于 Invoke-Webrequest $site -UseBasicParsing
$html = new-object -ComObject "HTMLFile; $html.IHTMLDocument2_write($site.rawcontent)
对于Invoke-RestMethod$site
$html = new-object -ComObject "HTMLFile; $html.IHTMLDocument2_write($site)
现在你可以正常解析了
例如通过 id
获取一个元素
$button = $html.getElementById('button')
通常 Invoke-WebRequest
更适合与 -UseBasicParsing
参数一起使用,如果不使用 Invoke-RestMethod
命令以提高性能和网络节省。
但是这些结果没有好的 ol'PARSEHTML
方法。
我们如何使用规定的命令设置解析 html?
Invoke-Webrequest $site -UseBasicParsing
Invoke-RestMethod $site
可以通过创建一个新的 HTML 对象并写入其 IHTMLDocument2 部分
来解决该场景注意:这仅在 WINDOWS POWERSHELL 5.0 和 5.1
中有效您可以按照以下方式处理列出的场景:
对于 Invoke-Webrequest $site -UseBasicParsing
$html = new-object -ComObject "HTMLFile; $html.IHTMLDocument2_write($site.rawcontent)
对于Invoke-RestMethod$site
$html = new-object -ComObject "HTMLFile; $html.IHTMLDocument2_write($site)
现在你可以正常解析了 例如通过 id
获取一个元素$button = $html.getElementById('button')