【问题标题】:HTML table to databaseHTML表格到数据库
【发布时间】:2021-12-08 05:46:02
【问题描述】:

此时,我的表格如下所示:

<table border="0" cellpadding="0" cellspacing="0" class="ms-formtable" id="formTbl" style="margin-top: 8px;" width="100%">
  <tbody>
  <tr>
    <td class="ms-formlabel" nowrap="true" valign="top" width="165px">
      <h3 class="ms-standardheader">
        <a name="SPBookmark_FileLeafRef">
        </a>
        Name
      </h3>
    </td>
    <td class="ms-formbody" id="SPFieldFile" valign="top" width="450px">
      <a href="http://google.com" onclick="DispDocItemEx(this, 'FALSE', 'FALSE', 'FALSE', '');">
        X
      </a>
    </td>
  </tr>
  <tr>
    <td class="ms-formlabel" nowrap="true" valign="top" width="165px">
      <h3 class="ms-standardheader">
        <a name="SPBookmark_Owner">
        </a>
        Name@
      </h3>
    </td>
    <td class="ms-formbody" id="SPFieldChoice" valign="top" width="450px">
      Z
    </td>
  </tr>
  <tr>
    <td class="ms-formlabel" nowrap="true" valign="top" width="165px">
      <h3 class="ms-standardheader">
        <a name="SPBookmark_DirectiveRank">
        </a>
        Age
      </h3>
    </td>
    <td class="ms-formbody" id="SPFieldChoice" valign="top" width="450px">
      52
    </td>
  </tr>
  <tr>
    <td class="ms-formlabel" nowrap="true" valign="top" width="165px">
      <h3 class="ms-standardheader">
        <a name="SPBookmark_Number">
        </a>
        number
      </h3>
    </td>
    <td class="ms-formbody" id="SPFieldText" valign="top" width="450px">
      1
    </td>
  </tr>
  <tr>
    <td class="ms-formlabel" nowrap="true" valign="top" width="165px">
      <h3 class="ms-standardheader">
        <a name="SPBookmark_Title">
        </a>
        Name of File
      </h3>
    </td>
    <td class="ms-formbody" id="SPFieldText" valign="top" width="450px">
      Funny Names
    </td>
  </tr>
  <tr>
    <td class="ms-formlabel" nowrap="true" valign="top" width="165px">
      <h3 class="ms-standardheader">
        <a name="SPBookmark_EffectiveFrom">
        </a>
         date
      </h3>
    </td>
    <td class="ms-formbody" id="SPFieldDateTime" valign="top" width="450px">
      1.1.2022
    </td>
  </tr>
  </tbody>
</table>

我基本上需要打开一个 HTML 文件,过滤 id 为“formTbl”的表,然后使用值创建 JSON:{Firsttd:Secondtd, "Name":"Test", "Date":"Blank"} 或插入到 First td 的数据库中(在 tr 标签中,我们有 2 个 td,首先它的名称为列和第二个是表A中的值)和表B中的第二个td。有什么办法吗?我尝试过使用 Python,到目前为止,json 看起来像 [["","Name","","Test",""],["","Age","","12",""]],在 C# 中我尝试过 HTMLAgilityPack,但它不起作用。

【问题讨论】:

    标签: python c# html database html-agility-pack


    【解决方案1】:

    这是使用 JQuery 的解决方案。

    <html>
        <body>
        
            <table id="example-table">
                <tr>
                    <th>Name</th>
                    <th>Name@</th>
                    <th>Age</th>
                    <th>Number</th>
                    <th>Name of file</th>
                    <th>Date</th>
                </tr>
                <tr>
                    <td>X</td>
                    <td>Z</td>
                    <td>52</td>
                    <td>1</td>
                    <td>Name of file</td>
                    <td>2021-22-10</td>
                </tr>
            </table>
        
            <textarea rows="10" cols="50" id="jsonTextArea">
                
            </textarea>
        
        </body>
        </html>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/table-to-json@1.0.0/lib/jquery.tabletojson.min.js"></script>
        
        <script type="text/javascript">
        
            var tableToJson = $('#example-table').tableToJSON();
        
            var sendingData = JSON.stringify (tableToJson);
        
            $('#jsonTextArea').val(sendingData);
        
        
        // Send JSON data to backend
        $.post('http://localhost/test.php', {sendingData}, function(data, textStatus, xhr) {
            var backendResponse = data;
            console.log(backendResponse);       
        });
        
        </script>
    

    【讨论】:

    • 我需要上传大约 3000 个 html 页面,并且需要通过添加链接来完成,就像这样 for (int i = 0; i &lt;3000; i++) { send data from table into database} 每个页面都有相同的表,只是有差异。价值观
    • 我的桌子和我提到的一样(&lt;table&gt; &lt;tr&gt; &lt;td&gt; Column&lt;/td &lt;td&gt; Value &lt;/td&gt; &lt;/tr&gt; ..... &lt;/table>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2019-04-06
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多