Clappy101
Clappy101

Reputation: 23

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>

Upvotes: 2

Views: 12017

Answers (2)

Frode F.
Frode F.

Reputation: 54881

If there are only one link with 'product-url' class you could use:

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

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

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

Upvotes: 3

Related Questions