Yusuf
Yusuf

Reputation: 160

How to get a URL from the href attribute

I can't get URLs from href attributes. I use this code

Dim url As String = "http://example.com/"
Dim web As New HtmlWeb()
Dim doc As HtmlDocument = web.Load(url)

For Each item As HtmlNode In doc.DocumentNode.SelectNodes("//a/@href")
    If Not item Is Nothing Then
        Response.Write(item.OuterHtml)
    End If
Next

But it doesn't work.

Upvotes: 0

Views: 1246

Answers (1)

Sunil
Sunil

Reputation: 3424

Since href is an attribute you need to put it in square brackets []

Remember attributes go in to square brackets when you are searching by them.

//a[@href]

In your case you need to get all //a nodes, then check for HasAttributes("href") and finally, get Attributes("href").

For Each item As HtmlNode In doc.DocumentNode.SelectNodes("//a")
    If Not item Is Nothing And item.HasAttributes("href") Then
        Response.Write(item.Attributes("href").Value)
    End If
Next

Upvotes: 2

Related Questions