[使用PHP简单HTML DOM解析器查找带有类的div

问题描述 投票:5回答:3

我只是从提到的解析器开始,并以某种方式直接从头开始处理问题。

参考本教程:

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]');

但是现在,当源代码在div内没有更多内容时,当我print_r $ divs时,它会提供更多的功能。

喜欢这个:

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>

我在做什么错?

我只是从提到的解析器开始,并以某种方式直接从头开始解决问题。参考本教程:http://net.tutsplus.com/tutorials/php/html-parsing-and-screen -...

php parsing dom simple-html-dom
3个回答
7
投票

获得带有类的div的正确代码是:


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

0
投票

DIV->类(产品内部clearfix)->类(价格)

© www.soinside.com 2019 - 2024. All rights reserved.