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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15761115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:54:56  来源:igfitidea点击:

Find div with class using PHP Simple HTML DOM Parser

phpparsingdomsimple-html-dom

提问by Owl

I am just starting with the mentioned Parser and somehow running on problems directly with the beginning.

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

Referring to this tutorial:

参考本教程:

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

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

I want now simply find in a sourcecode tne content of a div with a class ClearBoth Box

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

I retrieve the code with curl and create a simple html dom object:

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

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

Then I wanted to add the content of the div into an array called divs:

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

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

But now, when I print_r the $divs, it gives much more, despite the fact that the sourcecode has not more inside the div.

但是现在,当我打印 $divs 时,它提供了更多,尽管源代码在 div 中没有更多。

Like this:

像这样:

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
                                        (
                                        )

I do not understand why the $divs has not simply the code from the div?

我不明白为什么 $divs 不只是来自 div 的代码?

Here is an example of the source code at the site:

这是网站上的源代码示例:

<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>

What am I doing wrong?

我究竟做错了什么?

回答by tampe125

The right code to get a div with class is:

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

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

Basically you can get elements as you were using a CSS selector.

基本上,您可以像使用 CSS 选择器一样获取元素。

source: http://simplehtmldom.sourceforge.net/manual.htm
How to find HTML elements?section, tab Advanced

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

回答by Mohit

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

回答by Hashan Chanaka

The to find the following elements: DIV -> class(product-inner clearfix) -> class(price)the following XPath can be used:

找到以下元素:DIV -> class(product-inner clearfix) -> class(price)可以使用以下 XPath:

foreach($html->find('div[class=product-inner  clearfix]') as $element){
        $itemPrice = $element->find('.price',0)->plaintext;
        echo $itemPrice;
    }