我一直在尝试使用invoke-Webrequest和“ParsedHtml.getElements”
ParsedHtml.getElementsByTagName("div") | Where{ $_.className -eq 'pricingContainer-priceContainer' } ).innerText
尝试获取值$8.29
但在下面的代码中使用它不会产生任何结果。我做错了什么?
<div class="pricingContainer pricingContainer--grid u-ngFade noCenterTag" ng-class="::{'noCenterTag': !showCenterTag}" ng-if="::featuresEnabled">
<!-- ngIf: ::(product.IsOnSpecial && !product.HideWasSavedPrice) -->
<div class="pricingContainer-priceContainer">
<span class="pricingContainer-priceAmount" ng-class="::specialClass">$8.29</span>
<!-- ngIf: ::product.CupPrice --><span ng-if="::product.CupPrice" class="pricingContainer-priceCup">
$5.19 / 100G
</span><!-- end ngIf: ::product.CupPrice -->
</div>
</div>
答案 0 :(得分:0)
通过类替换className:
($html.getElementsByTagName("span") | Where{ $_.class -eq 'pricingContainer-priceCup' }).innerText
或
($html.getElementsByTagName("div") | Where{ $_.class -eq 'pricingContainer-priceContainer' }).innerText
一个例子:
$Site = "http://example.com/index.html"
$all = Invoke-WebRequest -URI $Site
# $all contains all informations of the page
$html = [xml]$all.Content
#[xml] is a cast to convert code to xml
$html.getElementsByTagName("div")
您可以在IE中使用自动化。你选择一个包含Card的div女巫你可以得到这样的innerHTML:
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("http://www.example.com/index.html")
$ie.Visible = $true
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 2000; }
$html= $ie.Document.body.getElementsByTagName('div') | Where-Object {$_.className -eq "cardList-cards cardList-isotopeContainer"}
$lines = $html.innerHTML.split("`n")
$prices = $lines | Where-Object { $_ -Match '<span class=\"pricingContainer\-priceAmount\"' }
$prices = $prices | foreach { [regex]::matches($_, '>([0-9.$]*)</span>').Groups[1].Value }
echo $prices
答案 1 :(得分:0)
通过打开网页来解决这个坏孩子,等待正确的html加载动态html然后转储到txt文件进行阅读和搜索。
$path = "c:\sourcecode.txt"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("blahblahblahblah insert webpage here")
while($ie.ReadyState -ne 4) {start-sleep -s 10}
$ie.Document.body.outerHTML | Out-File -FilePath $path
$pricebf = select-string -path $path -pattern "pricingContainer-priceAmount" | select-object -First 1 | select Line
$Descriptionbf = select-string -path $path -pattern "canOpenDetail --><a title=" | select-object -First 1 | select Line