C#如何区分HTML标签?

时间:2015-11-05 23:35:20

标签: c# html web-scraping

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

来自网站的

代码段:

    // 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;                                                }

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

3 个答案:

答案 0 :(得分:0)

您可以使用像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元素,而不会出现正则表达式的困难:)

答案 1 :(得分:0)

我认为您可以尝试使用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
}

答案 2 :(得分:0)

因为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)