簡體   English   中英

如何在 php 中的簡單 html dom 中創建 dom 表?

[英]how to dom table in simple html dom in php?

嗨,我有這個 html 代碼,我想獲得 76.000.000.000 的數據。 我搜索了 div#TopBox 但它什么也沒顯示。(意味着沒有找到孩子) html 代碼:

<div id="TopBox">
<div id="divSupervision" style="color:#800040;font-size:12px;vertical-align:middle;">
</div>
<div class="box2 zi1">
   <div class="box6 h80">
      <table>
         <tbody>
            <tr>
               <td>Total</td>
               <td>
                  <div class="ltr inline" title="76,000,000,000">76 B</div>
               </td>
            </tr>
         </tbody>
      </table>
   </div>
   </div>
</div>

我的代碼:

$html = str_get_html($curl);
$div = $html->find("div#TopBox");

我不知道 'str_get_html' function 有什么問題,這似乎是 WP 的方法,但您可以使用 RegExp 獲取此數據:

preg_match('/title="([\d,]+)"/', $html, $matches);
var_dump($matches[1]);

您可以在div#TopBox中找到 div 並獲取其屬性title

$html = str_get_html($curl);
$total = $html->getElementById("TopBox")->find('div.inline')[0]->getAttribute('title');
echo $total; // 76,000,000,000

請注意,您可能會在訪問元素( [0] )之前測試數組的長度。

此代碼適用於本地 html

$html = file_get_html("file.txt");
$total = $html->getElementById("TopBox")->find('div.inline')[0]->getAttribute('title');
echo $total; // 76,000,000,000

但是當我使用它在線獲取數據時它不起作用,為什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM