0

ウェブサイトからタイムシートを取得する必要があります。このタイムシートを C# アプリケーションのデータ テーブルに保存/追加したいと考えています。

データ テーブルの構造は次のようになります。

1. | 日| 時間 | ステータス |
2...1.......7:00....IN
3...1.......9:45.......アウト
4...1......10:15....IN
5. ..1......15:45......OUT
6. ...1.......8:45.....合計
7. ..2 .. ..

DataTable の私の C# コード:

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

さまざまなバリエーションを試しましたが、常にすべてのデータを台無しにしています。

テスト目的で、「テキストボックス」(サイトパス用)と「ボタン」(プロセスを開始するため)を備えた新しいWinformを作成しました

次に、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. 問題: これは良い方法ではありません。そして、これは 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 を宣言することは問題ではありません。
主な問題は、適切な結果を吐き出すための htmlagility を取得できないことです。私が試した selectnodes のいくつかは、しばらくすると出力がおかしくなりました。まだ、ウェブサイトのテーブルから「すべて」のデータを取得することはできませんでした。いくつかの値だけですが、多くの場合バグがあります。
だから私は実際にこれを見て、正しい選択ノードを見つけるのを手伝ってくれる人を探しています。

4

1 に答える 1

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
于 2012-09-13T12:17:55.153 に答える