使用PHP DOM文档,按类选择HTML元素并获取其文本

时间:2022-11-12 10:49:57

I trying to get text from div where class = 'review-text', by using PHP's DOM element with following HTML (same structure) and following code.

我试图通过使用PHP的DOM元素和以下HTML(相同的结构)和下面的代码从div中获取文本,其中class ='review-text'。

However this doesn't seem to work

然而,这似乎不起作用

  1. HTML

    $html = '
        <div class="page-wrapper">
            <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
                <article class="review clearfix">
                    <div class="review-content">
                        <div class="review-text" itemprop="reviewBody">
                        Outstanding ... 
                        </div>
                    </div>
                </article>
            </section>
        </div>
    ';
    
  2. HTML $ html ='     

            
                
                    
                        
                        杰出......                                                                  “;

  3. PHP Code

        $classname = 'review-text';
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $xpath     = new DOMXPath($dom);
        $results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
    
        if ($results->length > 0) {
            echo $review = $results->item(0)->nodeValue;
        }
    
  4. PHP代码$ classname ='review-text';     $ dom = new DOMDocument;     $ dom-> loadHTML($ HTML);     $ xpath = new DOMXPath($ dom);     $ results = $ xpath-> query(“// * [@class and contains(concat('',normalize-space(@class),''),'$ classname')]”);     if($ results-> length> 0){         echo $ review = $ results-> item(0) - > nodeValue;     }

The XPATH syntax to select element by Class is provided at this Blog

此博客提供了按类选择元素的XPATH语法

I have tried many example from *, online tutorials, but none seems to work. Am I missing something ?

我在*,在线教程中尝试过很多例子,但似乎都没有。我错过了什么吗?

2 个解决方案

#1


25  

The following XPath query does what you want. Just replace the argument provided to $xpath->query with the following:

以下XPath查询执行您想要的操作。只需用以下代码替换提供给$ xpath-> query的参数:

//div[@class="review-text"]

Edit: For easy development, you can test your own XPath query's online at http://www.xpathtester.com/test.

编辑:为了便于开发,您可以在http://www.xpathtester.com/test上在线测试自己的XPath查询。

Edit2: Tested this code; it worked perfectly.

Edit2:测试了这段代码;它工作得很好。

<?php

$html = '
    <div class="page-wrapper">
        <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
            <article class="review clearfix">
                <div class="review-content">
                    <div class="review-text" itemprop="reviewBody">
                    Outstanding ... 
                    </div>
                </div>
            </article>
        </section>
    </div>
';

$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class='" . $classname . "']");

if ($results->length > 0) {
    echo $review = $results->item(0)->nodeValue;
}

?>

#2


4  

Expanding on Frak Houweling answer, it is also possible to use DomXpath to search within a specific DomNode. This can be acheived by passing the contextNode as a second argument to DomXpath->query method:

扩展Frak Houweling的答案,也可以使用DomXpath在特定的DomNode中进行搜索。这可以通过将contextNode作为第二个参数传递给DomXpath-> query方法来实现:

$dom = new DOMDocument;
$dom->loadHTML ($html);
$xpath = new DOMXPath ($dom);

foreach ($xpath->query ("//section[@class='page single-review']") as $section)
{
    // search for sub nodes inside each element
    foreach ($xpath->query (".//div[@class='review-text']", $section) as $review)
    {
        echo $review->nodeValue;
    }
}

Note that when searching inside nodes you need to use relative paths by adding a dot . at the beginning of the expression:

请注意,在内部节点中搜索时,您需要通过添加点来使用相对路径。在表达的开头:

"//div[@class='review-text']" // absolute path, search starts from the root element
".//div[@class='review-text']" // relative path, search starts from the provided contextNode

#1


25  

The following XPath query does what you want. Just replace the argument provided to $xpath->query with the following:

以下XPath查询执行您想要的操作。只需用以下代码替换提供给$ xpath-> query的参数:

//div[@class="review-text"]

Edit: For easy development, you can test your own XPath query's online at http://www.xpathtester.com/test.

编辑:为了便于开发,您可以在http://www.xpathtester.com/test上在线测试自己的XPath查询。

Edit2: Tested this code; it worked perfectly.

Edit2:测试了这段代码;它工作得很好。

<?php

$html = '
    <div class="page-wrapper">
        <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
            <article class="review clearfix">
                <div class="review-content">
                    <div class="review-text" itemprop="reviewBody">
                    Outstanding ... 
                    </div>
                </div>
            </article>
        </section>
    </div>
';

$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class='" . $classname . "']");

if ($results->length > 0) {
    echo $review = $results->item(0)->nodeValue;
}

?>

#2


4  

Expanding on Frak Houweling answer, it is also possible to use DomXpath to search within a specific DomNode. This can be acheived by passing the contextNode as a second argument to DomXpath->query method:

扩展Frak Houweling的答案,也可以使用DomXpath在特定的DomNode中进行搜索。这可以通过将contextNode作为第二个参数传递给DomXpath-> query方法来实现:

$dom = new DOMDocument;
$dom->loadHTML ($html);
$xpath = new DOMXPath ($dom);

foreach ($xpath->query ("//section[@class='page single-review']") as $section)
{
    // search for sub nodes inside each element
    foreach ($xpath->query (".//div[@class='review-text']", $section) as $review)
    {
        echo $review->nodeValue;
    }
}

Note that when searching inside nodes you need to use relative paths by adding a dot . at the beginning of the expression:

请注意,在内部节点中搜索时,您需要通过添加点来使用相对路径。在表达的开头:

"//div[@class='review-text']" // absolute path, search starts from the root element
".//div[@class='review-text']" // relative path, search starts from the provided contextNode