【问题标题】:C# How to tell the difference of HTML tags?C# 如何区分 HTML 标签?
【发布时间】:2015-11-07 06:15:16
【问题描述】:

我正在制作一个程序,从网站获取足球统计数据并存储它。问题是网站在 HTML 代码中存储不同状态的方式没有区别。

来自网站的代码 sn-p:

    // First Team
    <td style="background-color:#79a6ca;"><!-- --></td>
            <td class="t_c" style="border-right:1px #dddddd solid; background-color:#ebf2f7;">2</td>
            <td style="padding-left:10px; background-color:#ebf2f7;"><img src="/assets/images/logo/participants/16x16/8113.png" width="16" height="16" border="0" alt="FC Midtjylland" title="FC Midtjylland" /> <a href="/fodboldklubber/fc-midtjylland/" style="font-weight:bold; color:#333;">FC Midtjylland</a></td>
            <td class="t_c" style="background-color:#ebf2f7;">14</td>
            <td class="t_c" style="background-color:#ebf2f7;">8</td>
            <td class="t_c" style="background-color:#ebf2f7;">3</td>
            <td class="t_c" style="background-color:#ebf2f7;">3</td>
            <td class="t_c" style="border-left:1px #dddddd solid; border-right:1px #dddddd solid; background-color:#ebf2f7;">19 - 10</td>
            <td class="t_c" style="font-weight:bold; background-color:#ebf2f7;">27</td>
            </tr>
// Second team
                    <tr data-toggle="tooltip" data-placement="left" title="Europa League kvalifikation">
            <td style="background-color:#79a6ca;"><!-- --></td>
            <td class="t_c" style="border-right:1px #dddddd solid; background-color:#ebf2f7;">3</td>
            <td style="padding-left:10px; background-color:#ebf2f7;"><img src="/assets/images/logo/participants/16x16/8595.png" width="16" height="16" border="0" alt="Brøndby IF" title="Brøndby IF" /> <a href="/fodboldklubber/broendby-if/" style="font-weight:bold; color:#333;">Brøndby IF</a></td>
            <td class="t_c" style="background-color:#ebf2f7;">14</td>
            <td class="t_c" style="background-color:#ebf2f7;">7</td>
            <td class="t_c" style="background-color:#ebf2f7;">3</td>
            <td class="t_c" style="background-color:#ebf2f7;">4</td>
            <td class="t_c" style="border-left:1px #dddddd solid; border-right:1px #dddddd solid; background-color:#ebf2f7;">24 - 17</td>
            <td class="t_c" style="font-weight:bold; background-color:#ebf2f7;">24</td>
            </tr>

我正在使用 WebClient 来下载页面并使用 MatchCollection 来搜索所需的模式。 计划是将值写入字符串数组。

我已经尝试了 JStromwick 示例,它有点工作,但它并没有在团队之后停止。它还需要下一个团队。我该如何解决这个问题。可以放柜台吗?

到目前为止我的代码:

string[] superLigaHold = new string[] { "FC Midtjylland", "Brøndby IF" };
for (int i = 0; i < superLigaHold.Length; i++)
            {
                string teamPattern = "<img src.*? width=\"16\" height=\"16\" border=\"0\" alt=\"" + superLigaHold[i] + "\" title=\"" + superLigaHold[i] + "\" />";
                MatchCollection team = Regex.Matches(webPage, teamPattern, RegexOptions.Singleline);               
                if (team.Count > 0)
                {
                        var gameStats = Regex.Matches(webPage, "<td.+?>(\d+).*");              
                        string gamesTotal = gameStats[0].Groups[1].Value;
                        string gamesWon = gameStats[1].Groups[1].Value;
                        string gamesDraw = gameStats[2].Groups[1].Value;
                        string gamesLost = gameStats[3].Groups[1].Value;                                                }

有人对我如何解决这个问题有任何建议吗?

【问题讨论】:

  • 我不是 C# 程序员,但如果你只得到那个 html 而仅此而已,我不知道这是可能的:/.. 这个 html 总是按这个顺序排列吗?

标签: c# html web-scraping


【解决方案1】:

因为 HTML 中没有任何其他信息,所以您唯一可以考虑的就是列顺序。如果您将上述 HTML 作为字符串,则可以使用带有捕获组的正则表达式来查找您要查找的值。比如:

var html = 
    @"<td class=""t_c"" style=""background-color:#f2faf2;"">14</td> // Total matches
    <td class=""t_c"" style=""background-color:#f2faf2;"">9</td> // Won matches
    <td class=""t_c"" style=""background-color:#f2faf2;"">3</td> // Draw matches
    <td class=""t_c"" style=""background-color:#f2faf2;"">2</td> // Lost matches";

var matches = Regex.Matches(html, @"<td.+?>(\d+).*");

var totalMatches = matches[0].Groups[1].Value;
var wonMatches = matches[1].Groups[1].Value;
var drawMatches = matches[2].Groups[1].Value;
var lostMatches = matches[3].Groups[1].Value;

您可以从 http://www.regular-expressions.info/dotnet.html

我发现http://regexhero.net/tester/ 是一个方便的测试工具(需要 Silverlight)

【讨论】:

    【解决方案2】:

    我认为你可以尝试使用htmlagilitypack

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);
    
    HtmlNode bodyContent = doc.DocumentNode.SelectSingleNode("//body");   
    var all_td = bodyContent.SelectNodes("//td");
    
    foreach (var node in all_td) 
    {
        //Put your code here
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 CSS 选择器引擎,例如 https://github.com/jamietre/CsQuery

      那么(在 css 方面),总匹配数将是:

      var matches = dom.Select(".t_c");
      string total_matches = matches[0].InnerText; //=first occurence of the class .t_c
      string won_matches = matches[1].InnerText;
      string draw_matches =matches[2].InnerText;
      string lost_matches =matches[3].InnerText;
      

      它还可以帮助您轻松解析其他 html 元素,而不会遇到正则表达式的困难:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多