Search code examples
htmlpowershellhref

Extracting HTML Links with href


I've used the below code:

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

to extract information from this HTML code:

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

but I can't seem to extract the URL from the following HTML code using .ParsedHtml.getElementsByTagName().

</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>

Solution

  • Select the <div> element, then select the <a> element(s) from the result:

    $HTML.ParsedHtml.getElementsByTagName('div') |
      Where-Object { $_.className -eq 'detail' } |
      ForEach-Object { $_.getElementsByTagName('a') } |
      Where-Object { $_.className -eq 'product-url' } |
      Select-Object -Expand href