9

这个问题似乎已经回答了很多次,但我似乎仍然无法将这些碎片拼凑在一起。

我想按名称获取每个类的节点值。例如

<td class="thename"><strong>32</strong></td>
<td class="thename"><strong>12</strong></td>

我想抓住 32 和 12。我认为这需要 for 循环,但不确定如何实现它。这是我到目前为止所拥有的

$domain = "http://domain.com";
$dom = new DOMDocument();

$dom->loadHTMLFile($domain);
$xpath = new DomXpath($dom);
$div = $xpath->query('//*[@class="thename"]')->item(0);
$stuff = $div ->textContent;

echo($stuff);
4

2 回答 2

13

这是你要找的吗?

    $result = array();

    $doc = <<< HTML
    <html>
        <body>
            <div>1
                <span>2</span>
            </div>
            <div>3</div>
            <div>4
                <span class="class1"><strong>5</strong></span>
                <span class="class1"><strong>6</strong></span>
                <span>7</span>
            </div>
        </body>
    </html>
HTML;
    $classname = "class1";
    $domdocument = new DOMDocument();
    $domdocument->loadHTML($doc);
    $a = new DOMXPath($domdocument);
    $spans = $a->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");

    for ($i = $spans->length - 1; $i > -1; $i--) {
        $result[] = $spans->item($i)->firstChild->nodeValue;
    }

    echo "<pre>";
    print_r($result);
    exit();
于 2013-03-03T10:45:42.297 回答
5

我只是在php中做了这个

$dom = new DOMDocument('1.0');
           $classname = "product-name";

           @$dom->loadHTMLFile("http://shophive.com/".$query);
           $nodes = array();
           $nodes = $dom->getElementsByTagName("div");
           foreach ($nodes as $element)
           {
               $classy = $element->getAttribute("class");
               if (strpos($classy, "product") !== false)
               {
                       echo $classy;
                       echo '<br>';
               }

           }
于 2016-01-28T10:10:59.277 回答