Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell返回HTML div的内容_Html_Powershell_Xml Parsing_Html Parsing - Fatal编程技术网

使用Powershell返回HTML div的内容

使用Powershell返回HTML div的内容,html,powershell,xml-parsing,html-parsing,Html,Powershell,Xml Parsing,Html Parsing,我有一个类似结构化HTML文件的目录,给出了两个示例: File-1.html <html> <body> <div class="foo">foo</div> <div class="bar"><div><p>bar</p></div></div> <div class="baz">baz</div&g

我有一个类似结构化HTML文件的目录,给出了两个示例:

File-1.html

<html>
    <body>
        <div class="foo">foo</div>
        <div class="bar"><div><p>bar</p></div></div>
        <div class="baz">baz</div>
    </body>
</html>
这将返回一个错误:

You cannot call a method on a null-valued expression. 
At C:\Users\Public\Documents\Sandbox\parse-html.ps1:9 char:2 
+     echo $content.ParsedHtml.getElementById("bar").innerHTML`
我不理解这个错误,因为bar是一个存在的HTML元素


我做错了什么?

您可以尝试以下方法:

 $content = Get-Content File-1.html
 $xmlContent = [xml]$content

 $bar = $xmlContent.html.body.div | where {$_.div -eq 'bar'}

 Write-Output $bar.InnerXML

$content有价值吗?该错误告诉您调用方法的变量为nullYes,当我执行echo$content时,返回文件-1.HTML的HTML。确定$content.ParsedHtml如何?因为$content没有名为ParsedHtml的属性,我认为powershell默认情况下不支持解析HTML文件。你可以尝试使用。或者你可以把这行当作字符串,试着用regexha获取标记的内容:对不起,我自己不是regex专家:我能给你的唯一提示是尝试regex101.com,它会显示语法错误和其他东西。请看这里:
You cannot call a method on a null-valued expression. 
At C:\Users\Public\Documents\Sandbox\parse-html.ps1:9 char:2 
+     echo $content.ParsedHtml.getElementById("bar").innerHTML`
 $content = Get-Content File-1.html
 $xmlContent = [xml]$content

 $bar = $xmlContent.html.body.div | where {$_.div -eq 'bar'}

 Write-Output $bar.InnerXML