简体   繁体   English

DOMXPath / DOMDocument-如何获取具有相同属性的许多元素的foreach

[英]DOMXPath/DOMDocument - How to fetch foreach of many elements with same attribute

I am using DOMXPath and DOMDocument to fetch data from HTML output source. 我正在使用DOMXPath和DOMDocument从HTML输出源获取数据。

Here is my code: 这是我的代码:

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$name = $xpath->query('//td[@data-column-name="Model"]')->item(0)->nodeValue;

When i echo $name i receive only the first result which this code is finding. 当我echo $name我仅收到此代码正在查找的第一个结果。 There are much more elements with <td class=" " data-column-name="Model"> but this code is giving me only the first result. <td class=" " data-column-name="Model">还有更多元素,但是这段代码只给我第一个结果。 Do i have to make any foreach or while loop and how to get all results ? 我是否必须进行任何foreach或while循环,以及如何获得所有结果?

Thanks in advance! 提前致谢!

Use a loop like: 使用如下循环:

foreach ($xpath->query('//td[@data-column-name="Model"]')) as $item) {
    echo $item->nodeValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM