2012-09-11 28 views
0

我需要從網站抓取時間表。我想在C#應用程序中將此時間表存儲/添加到數據表中。抓取時間表HTMLAgilityPack

數據表的結構是這樣的:在數據表

 
1. | Day | Time | Status | 
2. ..1.......7:00.........IN 
3. ..1.......9:45.......OUT 
4. ..1......10:15........IN 
5. ..1......15:45......OUT 
6. ..1.......8:45.....TOTAL 
7. ..2  ..  .. 

我的C#代碼:

DataTable table = new DataTable("Worksheet"); 
table.Columns.Add("Day"); 
table.Columns.Add("Time"); 
table.Columns.Add("Status"); 

我嘗試不同的變體,我總是搞砸了所有的數據。

出於測試目的,我做了一個新的Winform用「文本框」(用於sitepath)和「按鈕」(啓動該進程)

然後我想HTMLAgilityPack得到的所有數據。一個例子:

public string[] GREYsource; 

public Form1() 
{ 
    InitializeComponent(); 
} 

private void btnSubmit_Click(object sender, EventArgs e) 
{ 
    var doc = new HtmlAgilityPack.HtmlDocument(); 
    var fileName = txtPath.Text;     // I downloaded the HTML-File 
    doc.Load(fileName); 

    string strGREYInner; 

    foreach (HtmlNode td in doc.DocumentNode.SelectNodes("//tr[@class=\"tblDataGreyNH\"]")) 
    { 
     strGREYInner = td.InnerText.Trim(); 
     string shorted = strGREYInner.Replace("\t", ""); string shorted2 = shorted.Replace("\n\n\n\n", "\n\n\n"); string shorted3 = shorted2.Replace("\n\n\n", "\n\n"); string shorted4 = shorted3.Replace("\n\n", "\n"); 
     GREYsource = shorted4.Split(new Char[] { '\n', }); 
    } 

    foreach (string str in GREYsource) 
    { 
     ... 
    } 
} 
  1. 問題:結果包含了許多標籤頁(/ t)和換行(/ n)的我需要修剪。
  2. 問題:這不是一個好辦法,IMO。這隻會佔用Totaltimes。

它可以做得更好。

這僅僅是我嘗試了示例(其它碼只是去垃圾的樁)

我連接的HTML如下結構:

概述(圖片): http://www.abload.de/img/overviewzoj18.png

的位更深入:

<html> 
    <head> 
    </head> 
    <style type="text/css"> 
    </style> 
    <body id="body" onload="handleMenuOverlapLogo();onload_column_expand();;firstElementFocus();"> 

    <.. some (java)scripts>    /* has to be ignoered. not necessary */ 
    <.. some other divs>    /* has to be ignoered. not necessary */ 
    <div id="rowContent">    /* This <div> contains the content i need */ 
     <div id="titleTab">    /* Title is not necessary */ 
     </div>      
     <div id="rowContentInner">   /* Here the content starts */ 
     <table class="tblList"> 
      <tbody> 
      <tr>    /* not necessary */ 
      <tr class="tblHeader">  /* not necessary */ 
      <tr class="tblHeader">  /* not necessary */ 
      <tr class="tblDataWhiteNH"> /* IN : */ 
       <td class="tblHeader" style="font-weight: bold; text-align: right"> In </td> 
       <td nowrap="">  /* "tblDataWhiteNH" always contains 7 "td nowrap" 
       <td nowrap=""> 
       <td nowrap="">  /* Example: if it contains a value */ 
       <table width="100%" border="0" align="center"> 
       <tbody> 
        <tr> 
         <td width="25%" align="left"> </td> 
         <td nowrap="" width="50%" align="center"> 7:53 </td> /* value = 7:53 (THIS!) */ 
         <td width="25%" align="right"> </td> 
        </tr> 
        </tbody> 
       </table> 
       </td> 
       <td nowrap=""> 
       <td nowrap="">  /* Example: if it contains no value */ 
       <table width="100%" border="0" align="center"> 
        <tbody> 
        <tr> 
         <td width="25%" align="left"> </td> 
         <td nowrap="" width="50%" align="center">  /* no value = 0:00 (THIS!) */ 
         <td width="25%" align="right"> </td> 
        </tr> 
        </tbody> 
       </table> 
       </td> 
       <td nowrap=""> 
       <td nowrap=""> 
      <tr class="tblDataWhiteNH"> /* OUT : */ 
       <td class="tblHeader" style="font-weight: bold; text-align: right"> Out </td> 
       <td nowrap="">  /* "tblDataWhiteNH" always contains 7 "td nowrap". 
       <td nowrap=""> 
       <td nowrap="">  /* Example: if it contains a value */ 
       <table width="100%" border="0" align="center"> 
       <tbody> 
        <tr> 
         <td width="25%" align="left"> </td> 
         <td nowrap="" width="50%" align="center"> 7:53 </td> /* value = 7:53 (THIS!) */ 
         <td width="25%" align="right"> </td> 
        </tr> 
        </tbody> 
       </table> 
       </td> 
       <td nowrap=""> 
       <td nowrap="">  /* Example: if it contains no value */ 
       <table width="100%" border="0" align="center"> 
        <tbody> 
        <tr> 
         <td width="25%" align="left"> </td> 
         <td nowrap="" width="50%" align="center">  /* no value = 0:00 (THIS!) */ 
         <td width="25%" align="right"> </td> 
        </tr> 
        </tbody> 
       </table> 
       </td> 
       <td nowrap=""> 
       <td nowrap=""> 
      <tr class="tblDataGreyNH"> /* IN : */ 
      <tr class="tblDataGreyNH"> /* OUT : */ 
      ...    /* "tblDataGreyNH" is built up the same way like "tblDataWhiteNH". 
      ...    /* sometimes there could be more "tblDataWhiteNH" and "tblDataGreyNH". */ 
      ...    /* Usally there are just the "tblDataWhiteNH"(IN/OUT) */ 
      <tr class="tblHeader">  /* not necessary */ 
          /* It continues f.egs. with "tblDataWhite" if the last above header was a "tblDatagrey" */ 
          /* and versa vice ("grey" if there was a "white" before.) */ 
      <tr class="tblDataWhiteNH"> /* Worked : */ 
       <td class="tblHeader" style="font-weight: bold; text-align: right"> Total Time </td> 
       <td> 07:47 </td>  /* value = 7:47 (THIS!) */ 
       <td> 04:48 </td>  
       <td> 00:00 </td>  /* no value = 0:00 (THIS!) */ 
       <td> 00:00 </td>  
       <td> 07:42 </td>  
       <td> 00:00 </td>  
       <td> 00:00 </td>  
      </tr> 
      <tr class="tblDataGreyNH"> /* Total : */ 
       <td class="tblHeader" style="font-weight: bold; text-align: right"> Regular Time </td> 
       <td> 07:47 </td>  /* value = 7:47 (THIS!) */ 
       <td> 04:48 </td>  
       <td> </td>   /* no value = 0:00 (THIS!) */ 
       <td> </td>   
       <td> 07:42 </td>  
       <td> </td> 
       <td> </td> 
      </tr> 
      <tr class="tblHeader">  /* not necessary */ 
      <tr valign="top">  /* not necessary */ 
      </tbody> 
     </table> 
     </div> 
    </div> 
    </body> 
</html> 

原始的HTML的副本:http://time.wnb.dk/123/

我希望任何人都可以幫助我得到這個工作。


好吧,讓我解釋一下圖片。 https://www.abload.de/img/eeeqnuwu.png
在圖片上您可以看到網站+下面的表格,結果應該如何。

聲明Datatable不是問題。
主要問題是我無法得到正確的結果,如果是的話,它幾乎是越野車。 我試過的一些selectnodes在一段時間後得到了輸出。迄今爲止,我無法從網站上的表中獲得「全部」數據,僅僅是一些值,但通常是錯誤的。
所以我實際上在尋找一個可以看一下這個的人,也許幫我找到正確的selectnodes。

+0

因爲它的立場是很清楚的問題 - 什麼是你真正問?你究竟在哪裏卡住?你到底在尋找什麼幫助? – Oded

+0

對不起,我不清楚解釋,即時通訊不是完美的英文,而不是在C#中的親。我只是想從我張貼的HTML代碼中的C#數據表。 (datatable)行應該在c#中看起來像這樣:'table.Rows.Add(「Daynumber,Timevalue,Status」);'。我的問題是,我總是搞砸了所有的「TD」,我沒有得到一個有用的結果/數據表。看來我堅持在html節點。 –

+0

似乎還不清楚。我張貼了一張照片,希望能更好地解釋它。 –

回答

1

不知道我完全理解你想要做什麼,但這裏是一個示例代碼,應該可以幫助你開始。我強烈建議你看看XPATH瞭解它。

 HtmlDocument doc = new HtmlDocument(); 
     doc.Load(yourFile); 

     // get all TR with a specific class name, starting from root (/), and recursively (//) 
     foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//tr[@class='tblDataGreyNH' or @class='tblDataWhiteNH']")) 
     { 
      // get all TD below the current node with a specific class name 
      HtmlNode inOrOut = node.SelectSingleNode("td[@class='tblHeader']"); 
      if (inOrOut != null) 
      { 
       string io = inOrOut.InnerText.Trim(); 
       Console.WriteLine(io.ToUpper()); 
       if (io.Contains("Time")) 
       { 
        // normalize-space gets rid or whitespaces (\r,\n, etc.) 
        // text() gets the node's inner text 
        foreach (HtmlNode td in node.SelectNodes("td[normalize-space(@class)='' and normalize-space(text())!='' and normalize-space(text())!='00:00']")) 
        { 
         Console.WriteLine("value:" + td.InnerText.Trim()); 
        } 
       } 
      } 

      // gets all TD below the current node that define the NOWRAP attribute 
      HtmlNodeCollection tdNoWraps = node.SelectNodes("td[@nowrap]"); 
      if (tdNoWraps != null) 
      { 
       foreach (HtmlNode tdNoWrap in tdNoWraps) 
       { 
        string value = tdNoWrap.InnerText.Trim(); 
        if (value == string.Empty) 
         continue; 

        Console.WriteLine("value:" + value); 
       } 
      } 
     } 

它將輸出該從你的樣品頁:

IN 
value:7:47 
value:7:46 
value:7:45 
value:7:51 
OUT 
value:15:35 
value:15:33 
value:12:38 
value:8:59 
IN 
value:12:38 
value:8:59 
OUT 
value:15:35 
TOTAL TIME 
value:07:48 
value:07:47 
value:07:50 
value:01:08 
REGULAR TIME 
value:07:48 
value:07:47 
value:07:50 
value:01:08 
+0

哇,這就是我正在尋找..非常感謝你!順便說一句,我不知道你可以在Xpath中使用'或/和'(現在我知道什麼語法被稱爲)來解析它。你的回答幫了我很多。 –