我正在尝试解析网站以获取特定的ID号,我感兴趣的网站部分看起来像这样:
<div class="clearfix" id="topCurrentGamesList">
<div class="topCurrentGames" id="topCurrentGame38632" data-game-id="4154">
</div>
<div class="topCurrentGames" id="topCurrentGame38639" data-game-id="4161">
</div>
</div>
现在,我正在尝试将数字放在子项的ID中,在这种情况下名称为 38632 , 38639 。我下面的代码用于选择父div到两个所需的div,因为它有一个静态名称。
$gameID = $dom->getElementById( 'topCurrentGamesList' );
这两个孩子的div会不时得到一个新的id,我想提取它。它仍然是我应该使用的DOM,如果是,如何?或者是否有更优雅(更简单)的解决方案?
的print_r($游戏ID);显示这个:
DOMElement Object
(
[tagName] => div
[schemaTypeInfo] =>
[nodeName] => div
[nodeValue] =>
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => div
[baseURI] =>
[textContent] =>
)
答案 0 :(得分:0)
是的,使用DOMDocument和DOMXPath适合此任务。您只需循环遍历每个选定的节点并收集“id”属性:
<?php
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//div[@id="topCurrentGamesList"]/div[@class="topCurrentGames"]');
foreach ($nodes as $node) {
$id = preg_replace('/^topCurrentGame/', '', $node->getAttribute('id'));
// do something with $id
}