【问题标题】:Find div with class using PHP Simple HTML DOM Parser使用 PHP Simple HTML DOM Parser 查找带有类的 div
【发布时间】:2013-04-02 09:54:17
【问题描述】:

我只是从提到的 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>

我做错了什么?

【问题讨论】:

    标签: php parsing dom simple-html-dom


    【解决方案1】:
    $html = new simple_html_dom();   
    $html->load($output); 
    $items = $html->find('div.youclassname',0)->children(1)->outertext; 
    print_r($items);
    

    【讨论】:

      【解决方案2】:

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

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

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

      来源:http://simplehtmldom.sourceforge.net/manual.htm
      如何找到 HTML 元素? 部分,标签 高级

      【讨论】:

      • 非常感谢!现在我更进一步了!就我而言,因为类名分为两部分“ClearBoth Box”,我必须使用: div[class=ClearBoth Box] 因为 div.ClearBoth Box 在 div 之后搜索元素 Box,并且只有 div.ClearBoth 返回比我需要。
      • 如果我的 div 没有类名怎么办?我想要页面上的所有 div?
      • @amitchhajer 你要么找到一个唯一 ID 高于或低于你的 div 的元素,然后更多的是子方法,父方法,或者你打印你所在位置的外部文本(dom 对象)和计算您需要的 div 之前有多少个 div,并通过它的编号访问它。第 4 个 div = dom->find('div',3);
      • 如何打印 HTML 怎么做?
      【解决方案3】:

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

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

      【讨论】:

      • 以下是How do I write a good answer? 的一些指南。提供的这个答案可能是正确的,但它可以从解释中受益。仅代码答案不被视为“好”答案。来自review
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 2013-09-05
      • 1970-01-01
      相关资源
      最近更新 更多