繁体   English   中英

使用PHP Simple HTML DOM Parser查找带有类的div

[英]Find div with class using PHP Simple HTML DOM Parser

我只是从提到的Parser开始,并以某种方式直接在开始时运行问题。

参考本教程:

http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/

我现在想要在一个源代码中找到一个带有类ClearBoth Box的div的内容

我用curl检索代码并创建一个简单的html dom对象:

$cl = curl_exec($curl);  
$html = new simple_html_dom();
$html->load($cl);

然后我想将div的内容添加到一个名为divs的数组中:

$divs = $html->find('div[.ClearBoth Box]');

但是现在,当我print_r $ divs时,它提供了更多,尽管事实上源代码在div中没有​​更多。

像这样:

Array
(
    [0] => simple_html_dom_node Object
        (
            [nodetype] => 1
            [tag] => br
            [attr] => Array
                (
                    [class] => ClearBoth
                )

            [children] => Array
                (
                )

            [nodes] => Array
                (
                )

            [parent] => simple_html_dom_node Object
                (
                    [nodetype] => 1
                    [tag] => div
                    [attr] => Array
                        (
                            [class] => SocialMedia
                        )

                    [children] => Array
                        (
                            [0] => simple_html_dom_node Object
                                (
                                    [nodetype] => 1
                                    [tag] => iframe
                                    [attr] => Array
                                        (
                                            [id] => ShowFacebookButtons
                                            [class] => SocialWeb FloatLeft
                                            [src] => http://www.facebook.com/plugins/xxx
                                            [style] => border:none; overflow:hidden; width: 250px; height: 70px;
                                        )

                                    [children] => Array
                                        (
                                        )

                                    [nodes] => Array
                                        (
                                        )

我不明白为什么$ divs不仅仅是div中的代码?

以下是该站点源代码的示例:

<div class="ClearBoth Box">
          <div>
<i class="Icon SmallIcon ProductRatingEnabledIconSmall" title="gute peppige Qualität: Sehr empfehlenswert"></i>
<i class="Icon SmallIcon ProductRatingEnabledIconSmall" title="gute peppige Qualität: Sehr empfehlenswert"></i>
<i class="Icon SmallIcon ProductRatingEnabledIconSmall" title="gute peppige Qualität: Sehr empfehlenswert"></i>
<i class="Icon SmallIcon ProductRatingEnabledIconSmall" title="gute peppige Qualität: Sehr empfehlenswert"></i>
<i class="Icon SmallIcon ProductRatingEnabledIconSmall" title="gute peppige Qualität: Sehr empfehlenswert"></i>

              <strong class="AlignMiddle LeftSmallPadding">gute peppige Qualität</strong> <span class="AlignMiddle">(17.03.2013)</span>
          </div>
          <div class="BottomMargin">
            gute Verarbeitung, schönes Design,
          </div>
        </div>

我究竟做错了什么?

使用class获取div的正确代码是:

$ret = $html->find('div.foo');
//OR
$ret = $html->find('div[class=foo]');

基本上你可以在使用CSS选择器时获取元素。

来源: http//simplehtmldom.sourceforge.net/manual.htm
如何查找HTML元素? 部分,选项卡高级

$html = new simple_html_dom();   
$html->load($output); 
$items = $html->find('div.youclassname',0)->children(1)->outertext; 
print_r($items);

我怎么找到最后一个<div class>在带有 PHP 简单 HTML DOM 解析器的 HTML 文件中?</div><div id="text_translate"><p> 根据<a href="http://simplehtmldom.sourceforge.net/" rel="nofollow noreferrer">SIMPLE HTML DOM PARSER</a>的文档(在“How to modify HTML Elements”选项卡下),这段代码找到了<div class="hello">的第一个实例:</p><pre> $html = str_get_html('<div class="hello">Hello</div><div class="world">World</div>'); $html->find('div[class=hello]', 0)->innertext = 'foo'; echo $html; // Output: <div class="hello">foo</div><div class="world">World</div></pre><p> 如果我想在<div class="hello">的<em>最后一个</em>实例中插入 'foo' 怎么办,假设 HTML 代码有很多<div class="hello">实例。</p><p> 什么应该取代0 ?</p></div>

[英]How do I find the last <div class> in an HTML File with PHP Simple HTML DOM Parser?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM