使用href提取HTML链接

时间:2016-01-31 05:01:02

标签: html powershell href

我使用了以下代码:

$URI   = $webpage
$HTML  = Invoke-WebRequest -Uri $URI
$price = ($HTML.ParsedHtml.getElementsByTagName("div") | Where {
           $_.className -eq 'price'
         }).innerText

从此HTML代码中提取信息:

<div class="price">
  <span class="accessibility">Purchase price</span>
  <small>$</small>3.50
</div>

但我似乎无法使用.ParsedHtml.getElementsByTagName()从以下HTML代码中提取网址。

</a>
<div class="detail">    
    <span role="presentation" aria-hidden="true" class="brand">Steves Fresh</span>
    <span class="item" role="presentation" aria-hidden="true">
        <a role="presentation" aria-hidden="true" class="product-url" href="http://testurlnodetailsshown.com.au">
        Steves&nbsp;
        2 pack
        </a>

2 个答案:

答案 0 :(得分:2)

选择function createpopupdata(event) { var el = event.target; alert(el.id + " clicked, foo: " + el.dataset.foo + ", bar: " + el.dataset.bar); } var eventbar = document.createElement("Div"); eventbar.id = "eventa"; eventbar.dataset.foo = "FOO"; eventbar.dataset.bar = "BAR"; eventbar.className= "event"; eventbar.innerHTML = "Click here"; document.getElementsByTagName("body")[0].appendChild(eventbar); eventbar.onclick = createpopupdata;元素,然后从结果中选择<body id="body">元素:

<div>

答案 1 :(得分:0)

如果只有一个带有'product-url'类的链接,您可以使用:

$html.Links |
Where-Object { $_.class -eq 'product-url' } |
Select-Object -ExpandProperty href