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

Owl*_*Owl 5 php parsing dom simple-html-dom

我只是从提到的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);
Run Code Online (Sandbox Code Playgroud)

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

$divs = $html->find('div[.ClearBoth Box]');
Run Code Online (Sandbox Code Playgroud)

但是现在,当我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
                                        (
                                        )
Run Code Online (Sandbox Code Playgroud)

我不明白为什么$ 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>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

tam*_*125 7

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

$ret = $html->find('div.foo');
//OR
$ret = $html->find('div[class=foo]');
Run Code Online (Sandbox Code Playgroud)

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

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


小智 6

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