[RESOLVED] Extracting data from an HtmlElementCollection
Hi Guys,
I am having trouble extracting the data from my HtmlElementCollection. I am trying to retrieve the links from a site and have them print out onto a separate form's rich text field.
Code:
'create an elements collection to store the links of the search results
Dim resultsCollection As HtmlElementCollection
resultsCollection = wbCraigsList.Document.GetElementsByTagName("a")
From here I would like to loop through the collection and print them out onto the other form called "Results"
Code:
Results.Show()
For i As Integer = 0 To resultsCollection.Count Step 1
Results.rtxtResults.Text = wbCraigsList.Document.GetElementsByTagName("a").ToString
Next
How do I approach extracting the data from the HtmlElementsCollection "resultsCollection" and place it into the other Form "Results" text field?
Re: Extracting data from an HtmlElementCollection
It helps if you provide the html. Get the attribute and select it's inner text.
Re: Extracting data from an HtmlElementCollection
Thank you for your help. I didn't even consider the inner text portion of this...
Here is the code block of where I solved my issue for anyone who is curious
Code:
Try
Me.getUserCriteria()
'counts the number of links after the search result
numResults = wbCraigsList.Document.GetElementsByTagName("a").Count
'posts the count on the main form
lblNumOfResults.Text = numResults.ToString()
'create an elements collection to store the links of the search results
Dim resultsCollection As HtmlElementCollection
resultsCollection = wbCraigsList.Document.GetElementsByTagName("a")
Results.Show()
For i As Integer = 0 To numResults - 1 Step 1
Results.lbListResults.Items.Add(resultsCollection(i).InnerText)
Next
Re: [RESOLVED] Extracting data from an HtmlElementCollection
Your not thinking at all about this. You get the same collection twice, not to mention not using the correct loop. (noted your not actually using a RTB as stated.
vb Code:
Public Class Form1
Private Sub SomeThing()
Dim resultsCollection As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("a")
Debug.WriteLine(resultsCollection.Count)
For Each element In resultsCollection
' what ever
Next
End Sub
End Class
vb Code:
Public Class Form1
Private Sub SomeThing()
Dim elements = Me.WebBrowser1.Document.GetElementsByTagName("a")
If elements IsNot Nothing Then
Dim items =
(
From el
In elements.Cast(Of HtmlElement)()
Where el.InnerText <> String.Empty
Select el.InnerText
)
Me.ListBox1.Items.AddRange(items.ToArray)
End If
End Sub
End Class