本文介绍了如何从HTML元素类中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正试图从课堂上获取文字

元素


i'm trying to get text from class
the element

<div id="cp-0" class="caption-line" data-time="19.5">
   <div class="caption-line-time">0:19</div>
   <div class="caption-line-text">I used to bite my tongue and hold my breath  Scared to rock the boat and make a mess</div>
</div>





我尝试了什么:





What I have tried:

If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
Dim elems As HtmlElementCollection
                elems = WebBrowser1.Document.GetElementsByTagName("div")
                For Each elem As HtmlElement In elems
                    Dim classstr As String = elem.GetAttribute("class")
                    If ((classstr Is Nothing) And (classstr.Length <> 0)) Then
                        If classstr.ToLower().Equals("description") Then
                            Dim conStr As String = elem.GetAttribute("content")
                            PrintLine(1, conStr)
                        ElseIf classstr.ToLower().Equals("caption-line-text") Then
                            Dim conStr As String = elem.GetAttribute("content")
                            PrintLine(1, conStr)
                        End If
                    End If
                Next
                FileClose(1)
            End If
        End If

推荐答案

If ((classstr IsNot Nothing) And (classstr.Length <> 0)) Then


If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
Dim elems As HtmlElementCollection
                elems = WebBrowser1.Document.GetElementsByTagName("div")
                For Each elem As HtmlElement In elems
                    Dim classstr As String = ""
                       classstr = elem.domelement.className '<---------------------------Changed this line
                    If classstr <> "" Then
                        If classstr.ToLower() = "description" Then
                            Dim conStr As String = elem.innertext
                            PrintLine(1, conStr)
                        ElseIf classstr.ToLower() = "caption-line-text" Then
                            Dim conStr As String = elem.Innertext
                            PrintLine(1, conStr)
                        End If
                    End If
                Next
                FileClose(1)
            End If
        End If



这篇关于如何从HTML元素类中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:58