【问题标题】:Trying to parse and interact with content from a web page using PowerShell尝试使用 PowerShell 解析网页内容并与之交互
【发布时间】:2017-07-30 04:01:43
【问题描述】:

这是我放入 PowerShell 的内容:

PS > $source = "http://www.bing.com/search?q=sqrt(2)"
PS > $result = Invoke-WebRequest $source
PS > $resultContainer = $result.ParsedHtml.GetElementById("results_container")

这是我收到的错误信息:

The property 'ParsedHtml' cannot be found on this object. Verify that the property exists.                                                                                   At line:1 char:1                                                                                                                                                             + $resultContainer = $result.ParsedHtml.GetElementById("results_contain ...                                                                                                  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

【问题讨论】:

  • 我应该补充一点,我在 Mac 的终端中使用 PowerShell。

标签: macos powershell web-scraping powershell-core


【解决方案1】:

我不相信您可以在非 Windows 平台上使用 PowerShell 执行此操作(至少目前还不行)。为了解析 HTML 内容,PowerShell 使用 MSHTML.DLL 和/或其他在 Windows 之外不存在的 Internet Explorer/Edge 组件。请注意 GetElementById just proxies to the COM object 并且您的环境中没有 COM 对象。

您可以检查Invoke-WebRequest 返回的对象的RawContent 属性并自己解析该字符串以查找您想要的内容,但是使用正则表达式解析HTML 是一个非入门,因此您必须使用其他方法。

顺便说一句,我无法在您在示例中使用的页面上找到具有 idresults_container 的元素。

【讨论】:

    【解决方案2】:

    有效的方法(但有点混乱)是在 Powershell 中使用 AngleSharp 作为 .Net 程序集。在Powershell github issue 中也有建议。

    [string]$html = "<!DOCTYPE html>
    <html lang=en>
        <meta charset=utf-8>
        <meta name=viewport content=""initial-scale=1, minimum-scale=1, width=device-width"">
        <title>Error 404 (Not Found)!!1</title>
        <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
        <p><b>404.</b> <ins>That’s an error.</ins>
        <p>The requested URL <code>/error</code> was not found on this server.  <ins>That’s all we know.</ins>";
    
    #Loads assembly for angle sharp: https://stackoverflow.com/questions/39257572/loading-assemblies-from-nuget-packages 
    #WARNING: probably in a non-portable way.
    $standardAssemblyFullPath = (Get-ChildItem -Filter *.dll -Recurse (Split-Path (get-package AngleSharp).Source)).FullName | Where-Object {$_ -like "*standard*"}
    Add-Type -Path $standardAssemblyFullPath
    
    $parser = New-Object AngleSharp.Parser.Html.HtmlParser
    $document = $parser.Parse($html);
    
    $elements = $document.All | Where-Object {$_.id -eq "logo"};
    
    Write-Host $elements.OuterHtml
    

    【讨论】:

    • HtmlParserParser 命名空间的子级,而不是 Html 命名空间。 HtmlParser 班级在AngleSharp.Html.Parser.HtmlParser $parser = New-Object AngleSharp.Html.Parser.HtmlParser
    猜你喜欢
    • 2015-12-05
    • 2013-11-24
    • 2018-12-29
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多