Extract Title from html link

后端 未结 2 906
傲寒
傲寒 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 = "<a href=""/tothepage"" title=""the page"">The Link</a>."
            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 "<a>" element) and prevent it from throwing an error if there is no "title" attribute.

    0 讨论(0)
  • 2021-01-29 00:03

    With a regular expression, the group will contain it ([^"]*):

    title="([^"]*)"
    

    C#

    using System.Text.RegularExpressions;
    static void Main(string[] args)
        {
            string originalString = "<a href=\" / tothepage\" title=\"the page\">The Link</a>.";
            Regex rgx = new Regex("title=\"([^\"]*)\"", RegexOptions.IgnoreCase);
            Match match = rgx.Matches(originalString)[0];
            Console.WriteLine(match.Groups[1]);
            Console.ReadLine();
        }
    
    0 讨论(0)
提交回复
热议问题