【问题标题】:PHP Simple HTML DOM Parser how to get Third table using find methodPHP Simple HTML DOM Parser 如何使用 find 方法获取第三个表
【发布时间】:2013-03-01 00:49:36
【问题描述】:

我有如下结构的 HTML 代码。

如何使用 PHP Simple HTML DOM 查找方法从该 HTML 代码中获取第三个表的内容?

<table width="100%" cellspacing="0" cellpadding="0" border="0">
  <tbody>
   <tr>
     <td>
       <table width="100%" cellspacing="0" cellpadding="0" border="0">
        <tbody>
          <tr>
           <td>
              <table width="100%" cellspacing="0" cellpadding="0" border="0">
              .......
              </table>

          </td>
         <tr>
        ..........
     <td>
  <tr>

</tbody>
</table>

【问题讨论】:

    标签: php dom html-parsing simple-html-dom


    【解决方案1】:

    要获取第三个表,在 find 方法中传递第二个参数(索引从 0 开始)。

      $html = file_get_html(<your_file_url/html_code>);    
      $html->find("table", 2);
    

    【讨论】:

      【解决方案2】:

      表格是这样嵌套的:

      $dom->find("table", 0); # first table
      $dom->find("table table", 0); # second table
      $dom->find("table table table", 0); # third table
      

      【讨论】:

        【解决方案3】:

        只是一个想法,试试这个:

        // Find first <table> in first <td>
        $html = file_get_html('yours.htm');
        $var = $html->find('td', 0)->find('table', 0);
        

        【讨论】:

        • 谢谢。但它不可能与 ->find($pattern) 仅因为我将它与不同的不同数据一起使用。
        • 什么是'不同-不同的数据'是什么意思?我只是想知道。
        • @egigundari:什么是 file_get_html(),我没有得到任何参考。
        • @egigundari 我已经创建了用于从不同类型的 HTML 文件中获取数据/详细信息的脚本,我正在使用 $html->find($pattern) 方法
        • @Suresh Raidon 使用 'PHP Simple HTML DOM Parser' 我猜,你可以用谷歌搜索一下。
        【解决方案4】:

        我不太确定,但您可以尝试以下方法:

        $dom = new DomDocument;
        $dom->loadXML($YourHTML);
        
        //I have written as item(1) to point at the second table
        $params = $dom->getElementsByTagName('table')->item(1);
        

        【讨论】:

        • 谢谢。但它不可能使用 (PHP simple html dom parser) ->find($pattern) 只是因为我将它与不同的不同数据一起使用。
        猜你喜欢
        • 1970-01-01
        • 2014-10-26
        • 2012-08-12
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        相关资源
        最近更新 更多