Results 1 to 4 of 4

Thread: [RESOLVED] Extracting data from an HtmlElementCollection

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    24

    Resolved [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?

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Extracting data from an HtmlElementCollection

    It helps if you provide the html. Get the attribute and select it's inner text.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    24

    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

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub SomeThing()
    4.         Dim resultsCollection As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("a")
    5.  
    6.         Debug.WriteLine(resultsCollection.Count)
    7.  
    8.         For Each element In resultsCollection
    9.             ' what ever
    10.         Next
    11.     End Sub
    12. End Class

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub SomeThing()
    4.         Dim elements = Me.WebBrowser1.Document.GetElementsByTagName("a")
    5.  
    6.         If elements IsNot Nothing Then
    7.             Dim items =
    8.                 (
    9.                     From el
    10.                     In elements.Cast(Of HtmlElement)()
    11.                     Where el.InnerText <> String.Empty
    12.                     Select el.InnerText
    13.                 )
    14.             Me.ListBox1.Items.AddRange(items.ToArray)
    15.         End If
    16.     End Sub
    17.  
    18. 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
  •  



Click Here to Expand Forum to Full Width