【问题标题】:Parsing HTML Table in C#在 C# 中解析 HTML 表
【发布时间】:2012-10-11 21:20:08
【问题描述】:

我有一个包含表格的 html 页面,我想以 C# windows 形式解析该表格

http://www.mufap.com.pk/payout-report.php?tab=01

这是我尝试过的要解析的网页

> Foreach(Htmlnode a in document.getelementbyname("tr"))
{
    richtextbox1.text=a.innertext;
}

我已经尝试过这样的事情,但它不会以表格形式给我,因为我只是打印所有 trs 所以请帮助我解决这个thanx 对不起我的英语。

【问题讨论】:

    标签: c# parsing html-agility-pack html-table


    【解决方案1】:

    使用Html Agility Pack

    WebClient webClient = new WebClient();
    string page = webClient.DownloadString("http://www.mufap.com.pk/payout-report.php?tab=01");
    
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(page);
    
    List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table[@class='mydata']")
                .Descendants("tr")
                .Skip(1)
                .Where(tr=>tr.Elements("td").Count()>1)
                .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
                .ToList();
    

    【讨论】:

    • 感谢 L.B.删除 [@class='mydata'] 后,它作为通用代码工作......完美......
    • 如果表是分页的,那么如何通过抓取进入下一页?
    • @Dark_Knight 你在网址中看到?tab=01了吗?
    • 我刚刚得到 "Test Street\tTest Loc\tTest town\ttest\ttest\ttest\ttest\ttest\ttest\ttest\ttest\ttest" 使用这个作为一个字符串(因为只有一个行)这不应该在嵌套列表中吗?
    • 为我工作。可以使用“//table[2]”获取的第二个节点
    【解决方案2】:

    你的意思是这样的吗?

    foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table")) {
        ///This is the table.    
        foreach (HtmlNode row in table.SelectNodes("tr")) {
        ///This is the row.
            foreach (HtmlNode cell in row.SelectNodes("th|td")) {
                ///This the cell.
            }
        }
    }
    

    【讨论】:

    • 是的,我想在网页中以 html 表格格式显示列表视图控件中的每个单元格
    【解决方案3】:

    迟到了,但是使用普通 C# 代码完成您所要求的事情的方法可能如下

    /// <summary>
    /// parses a table and returns a list containing all the data with columns separated by tabs
    /// e.g.: records = getTable(doc, 0);
    /// </summary>
    /// <param name="doc">HtmlDocument to work with</param>
    /// <param name="number">table index (base 0)</param>
    /// <returns>list containing the table data</returns>
    public List<string> getTableData(HtmlDocument doc, int number)
    {
      HtmlElementCollection tables = doc.GetElementsByTagName("table");
      int idx=0;
      List<string> data = new List<string>();
    
      foreach (HtmlElement tbl in tables)
      {
        if (idx++ == number)
        {
          data = getTableData(tbl);
          break;
        }
      }
      return data;
    }
    
    /// <summary>
    /// parses a table and returns a list containing all the data with columns separated by tabs
    /// e.g.: records = getTable(getElement(doc, "table", "id", "table1"));
    /// </summary>
    /// <param name="tbl">HtmlElement table to work with</param>
    /// <returns>list containing the table data</returns>
    public List<string> getTableData(HtmlElement tbl)
    {
      int nrec = 0;
      List<string> data = new List<string>();
      string rowBuff;
    
      HtmlElementCollection rows = tbl.GetElementsByTagName("tr");
      HtmlElementCollection cols;
      foreach (HtmlElement tr in rows)
      {
        cols = tr.GetElementsByTagName("td");
        nrec++;
        rowBuff = nrec.ToString();
        foreach (HtmlElement td in cols)
        {
          rowBuff += "\t" + WebUtility.HtmlDecode(td.InnerText);
        }
        data.Add(rowBuff);
      }
    
      return data;
    }
    

    上面将允许您通过使用页面内的表“索引”(对未命名的表有用)或通过将“表”HtmlElement 传递给函数(更快但仅对命名表有用)从表中提取数据);请注意,我选择返回“列表”作为结果,并使用制表符分隔各个列数据;您可以轻松更改代码以您喜欢的任何其他格式返回数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 2012-06-11
      • 2011-11-09
      • 1970-01-01
      • 2013-07-31
      • 2011-06-23
      • 1970-01-01
      • 2012-02-18
      相关资源
      最近更新 更多