zoukankan      html  css  js  c++  java
  • HtmlAgilityPack开发

    官方网站:

    http://html-agility-pack.net/

    Nuget安装:

    Install-Package HtmlAgilityPack

    C# HTML Parser Examples:

    // From File
    var doc = new HtmlDocument();
    doc.Load(filePath);
    
    // From String
    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    
    // From Web
    var url = "http://html-agility-pack.net/";
    var web = new HtmlWeb();
    var doc = web.Load(url);

    C# HTML Selectors Examples:

    // With XPath    
    var value = doc.DocumentNode
        .SelectNodes("//td/input")
        .First()
        .Attributes["value"].Value;
        
    // With LINQ    
    var nodes = doc.DocumentNode.Descendants("input")
        .Select(y => y.Descendants()
        .Where(x => x.Attributes["class"].Value == "box"))
        .ToList();

    示例代码:

    HtmlWeb webClient = new HtmlWeb();
    HtmlDocument doc = webClient.Load("http://www.cnsos.net/weburl/");
    
    HtmlNodeCollection hrefList = doc.DocumentNode.SelectNodes(".//a[@href]");
    
    if (hrefList != null)
    {
        foreach (HtmlNode href in hrefList)
        {
            HtmlAttribute att = href.Attributes["href"];
            Console.WriteLine(att.Value);
        }
    }
  • 相关阅读:
    codeforces 484D D. Kindergarten(dp)
    codeforces 484B B. Maximum Value(二分)
    codeforces 484A A. Bits(贪心)
    51nod-1537 1537 分解(矩阵快速幂+找规律)
    大数取模
    小明的烦恼
    子网掩码
    How Many Tables
    N的N次方
    外星人的供给站
  • 原文地址:https://www.cnblogs.com/wzwyc/p/7296106.html
Copyright © 2011-2022 走看看