How to get a URL from the href attribute

html-agility-pack vb.net xpath

Question

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.

Accepted Answer

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

Popular Answer

@Sunil I used this method to get demo videos from lynda.com but it not works! and it gets 403 error

 HtmlNode videoNode = doc.DocumentNode.SelectSingleNode("//video[@class='player']");
                string firstsLink = videoNode.Attributes["data-src"].Value;
                List<string> secLink = firstsLink.Split(';').ToList();
                videoURL = (secLink[index: 0]); 

https://www.lynda.com/mocha-tutorials/mocha-5-Essential-Training/601820-2.html




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why