Results 1 to 2 of 2

Thread: Get the first element of specific tagname - HtmlElement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2017
    Posts
    26

    Get the first element of specific tagname - HtmlElement

    hello
    I tried the following code to get the contents of postbody


    PHP Code:
       For Each ss In webbrowser1.Document.GetElementsByTagName("div")
                     If 
    ss.GetAttribute("ClassName") = "postbody" Then

                         topicextract
    .text ss.innerhtml
                         o 
    += 1

                     End 
    If
                 
    Next 
    But I want to get postbody contents without clear and signature_div
    How can I do that, I have tried many ways to no avail
    Attached Images Attached Images  

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Get the first element of specific tagname - HtmlElement

    Right now you are grabbing the entire innerHTML of the div with the class postbody. It sounds like you want to grab the innerHTML of the first child inside the div with the class postbody.

    Try something like this:
    Code:
    Private Sub Main()
        Dim divPostBody = GetElementsByClassName(webbrowser1.Document, "postbody")
        o = divPostBody.Count()
    
        For Each div In divPostBody
            Dim firstDiv = div.FirstChild()
            topicextract.Text = firstDiv.InnerHtml
        Next
    End Sub
    
    Private Function GetElementsByClassName(document As HtmlDocument, className As String) As IEnumerable(Of HtmlElement)
        Dim elements = New List(Of HtmlElement)()
        For Each element As HtmlElement In document.All
            If element.GetAttribute("className").Split(" ").Contains(className) Then
                elements.Add(element)
            End If
        Next
        Return elements
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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