2015-11-05 88 views
-1

我正在製作一個從網站獲取足球統計數據並存儲它的程序。問題是網站在HTML代碼中存儲不同狀態的方式沒有區別。從網站C#如何判斷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> 

我使用Web客戶端下載頁面,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;            } 

沒有人有我怎樣才能解決這個問題有什麼建議?

+0

我不是一個C#程序員但我不知道這是可能的,如果你只是得到該HTML,沒有更多:/ ..是這個HTML總是在這個順序? –

回答

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)

0

您可以像使用https://github.com/jamietre/CsQuery

然後一個CSS選擇器引擎(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元素,而無需定期難度表達式:)

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 
}