|
-
Jun 27th, 2014, 09:55 AM
#1
Thread Starter
Junior Member
[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?
-
Jun 27th, 2014, 10:45 AM
#2
Re: Extracting data from an HtmlElementCollection
It helps if you provide the html. Get the attribute and select it's inner text.
-
Jun 27th, 2014, 11:18 AM
#3
Thread Starter
Junior Member
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
-
Jun 27th, 2014, 02:49 PM
#4
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|