Extract Title from html link

后端 未结 2 905
傲寒
傲寒 2021-01-28 23:45

I have the following HTML string:

The Link.  

How can I extract title from the HTML s

2条回答
  •  不知归路
    2021-01-28 23:59

    As you have the HtmlAgilityPack already, you can extract the "title" attribute like this:

    Option Infer On
    Option Strict On
    
    Imports HtmlAgilityPack
    
    Module Module1
    
        Sub Main()
            Dim a = "The Link."
            Dim doc As New HtmlDocument()
            doc.LoadHtml(a)
            Dim node = doc.DocumentNode.SelectSingleNode("/a")
            Dim title = node?.Attributes("title")?.Value
    
            Console.WriteLine(title) ' outputs "the page"
    
            Console.ReadLine()
    
        End Sub
    
    End Module
    

    Of course, you won't need that many lines of code as that is a complete working example.

    The ?. parts prevent it from throwing an error if node is Nothing (in this case if there wasn't an "" element) and prevent it from throwing an error if there is no "title" attribute.

提交回复
热议问题