以下是我想从
获取信息的HTML代码<div id="container">
<div id="classifiedDetail">
<div class="classifiedDetail">
<div class="classifiedDetailContent">
<div class="classifiedInfo">
<ul class="classifiedInfoList">
<li>
<strong>Number</strong>
<span class="classifiedId" id="classifiedId">434599923</span>
</li>
<li>
<strong>Date</strong>
<span> 30-9-2017 </span>
</li>
<li>
<strong>Number of cards</strong>
<span class=""> 23 </span>
</li>
<li>
<strong>Number of cubes</strong>
<span class=""> 0 </span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
我想访问第三个<li>
标记。我想从23
<span>
号
这是我试过的
$subPage = Invoke-WebRequest -Uri $someLink
$temp = $subPage.ParsedHtml.body.getElementsByTagName('div') |
Where-Object{$_.className -eq 'classifiedinfo'} |
Where {$_.getElementsByTagName('ul')} |
Where_Object {$_.className -eq 'classifiedInfoList'} |
Where {$_.getElementsByTagName('li')} |
Where {$_.getElementsByTagName('span')} |
Where-Object {$_.className -eq ''}
我正在尝试$temp | Out-File temp.txt
查看结果,但它没有给我任何结果。
答案 0 :(得分:2)
使用querySelector
获取第一个匹配元素:
$text = $subPage.ParsedHtml.querySelector(
'.classifiedInfo .classifiedInfoList li span[class=""]').textContent
使用querySelectorAll
获取所有匹配元素的文字:
function qsa($req, $selector) {
$collection = $req.ParsedHtml.querySelectorAll($selector)
foreach ($i in 0..($collection.length - 1)) { $collection.item($i) }
}
$req = Invoke-WebRequest http://localhost/test.html
$text = (qsa $req '.classifiedInfo .classifiedInfoList li span[class=""]').textContent
请注意,这些函数中的类名区分大小写。