【问题标题】:How to get content text of div by simple html dom - php如何通过简单的 html dom 获取 div 的内容文本 - php
【发布时间】:2020-04-02 04:01:17
【问题描述】:

我通过simple dom html (file_get_html('http://example.com')) 得到底部的html代码

<div id="ship" class="fe" data-feature-name="box" data-cel-widget="sox">
 <div class="a-medium b-di">
   <div id="mer-info" class="a-section a-spacing-mini">

    Hello World

     <span class="">          
    </span>   

  </div>
 </div>
</div>

如何获取“Hello World”内容文本?

我尝试了很多东西,例如底部文本,但这给了我 'NULL'

   $html->find('div[id="mer-info"]',0);
   $html->find("div#mer-info");
   $html->find("div#mer-info")->plaintext;
   $html->find('div[id="mer-info"]')->innertext;
   and ...

但我还是 NULL!

【问题讨论】:

  • 请提供您的代码示例。不清楚您如何使用file_get_html(),我测试了您的三种方法,其中两种确实有效,因此问题可能与您如何检索 html 有关。
  • @Nima 我更新了。我得到带有file_get_html 的HTML 代码 如何使用simple_html_dom.php 访问所有div 内容
  • 这个问题在这里可能已经有了答案...stackoverflow.com/questions/33053528/…

标签: php simple-html-dom


【解决方案1】:

您只将第二个参数 (0) 传递给 find 方法,您使用 div[id="mer-info"] 作为选择器,这似乎无法被 find 方法识别。请尝试以下操作:

require 'simple_html_dom.php';

$html =<<<html
<div id="ship" class="fe" data-feature-name="box" data-cel-widget="sox">
 <div class="a-medium b-di">
   <div id="mer-info" class="a-section a-spacing-mini">

    Hello World

     <span class="">
    </span>

  </div>
 </div>
</div>
html;

$dom = str_get_html($html);

$elem = $dom->find('#mer-info', 0);
print $elem->plaintext;
print "\n";

$elem = $dom->find('div#mer-info', 0);
print $elem->plaintext;

【讨论】:

  • 是的,我做到了,请像外部 URL 一样尝试。我的意思是通过file_get_html 获取 HTML,然后尝试,谢谢
  • 你确定file_get_html 得到正确的 HTML 吗?这就是我在推荐中想要说的,也许这不是你如何使用find 方法的问题,而是你如何获得html。请尝试我的代码,不要用file_get_html 替换str_get_html,看看它是否有效。如果是这样,那么您的问题可能出在file_get_html 函数上。
  • 哎呀,这在很多div 标签中可能是因为这个。我该怎么办?那是8个div的孩子,我的意思是有8个父母div
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
相关资源
最近更新 更多