Can you provide examples of parsing HTML?

后端 未结 29 2216
走了就别回头了
走了就别回头了 2020-11-22 13:49

How do you parse HTML with a variety of languages and parsing libraries?


When answering:

Individual comments will be linked to in answers to questions

29条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 14:20

    Language: C#
    Library: System.XML (standard .NET)

    using System.Collections.Generic;
    using System.Xml;
    
    public static void Main(string[] args)
    {
        List matches = new List();
    
        XmlDocument xd = new XmlDocument();
        xd.LoadXml("...");
    
        FindHrefs(xd.FirstChild, matches);
    }
    
    static void FindHrefs(XmlNode xn, List matches)
    {
        if (xn.Attributes != null && xn.Attributes["href"] != null)
            matches.Add(xn.Attributes["href"].InnerXml);
    
        foreach (XmlNode child in xn.ChildNodes)
            FindHrefs(child, matches);
    }
    

提交回复
热议问题