How do I get vb.net to add all links within a particular div class from a webpage

403 views Asked by At

I want to add in the list box items:

https://www.facebook.com/XXXXXXX

https://www.facebook.com/XXXXXXX

document:

<div class="fsl fwb fcb">
 <a href="https://www.facebook.com/XXXXXXX?fref=pb&hc_location=friends_tab"
<div class="fsl fwb fcb">
 <a href="https://www.facebook.com/XXXXXXX?fref=pb&hc_location=friends_tab"
<div class="fsl fwb fcb">
 <a href="https://www.facebook.com/XXXXXXX?fref=pb&hc_location=friends_tab"
1

There are 1 answers

3
Tim Schmelter On

This works as expected:

Dim html = File.ReadAllText("Path")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)

Dim anchorTexts As New List(Of String)
Dim divNodes = doc.DocumentNode.SelectNodes("//div[@class='fsl fwb fcb']")
If Not divNodes Is Nothing Then
    For Each div In divNodes
        For Each anchorNode In div.SelectNodes("//a")
            Dim href As String = anchorNode.GetAttributeValue("href", "")
            If Not String.IsNullOrEmpty(href) Then
                anchorTexts.Add(href)
            End If
        Next
    Next
End If