我正在尝试使用 dom 文档 xpath 查询获取数据,但在获取数据时遇到了一些问题。这是我的网页
<div class="abc">
<a href="xyz.php">
<span class="a1">Mexico</span>
<span class="a2">Canada</span>
<span class="a3">Brazil</span>
</a>
</div>
这是我的 html 代码。由此,我需要在类a1
、a2
、a3
中获取结果。为此,我这样写。
$nodes = $xpath->query("//*[@class='abc']");
谁能帮助我下一步继续。
首先,你的HTML被破坏了。关闭<a>
元素两次。
其次,解决了第一个问题,这并不难做到。您只需要搜索span
元素,其 class
属性等于其中一个:
$nodes = $xpath->query("//*[@class='abc']//span[@class='a1' or @class='a2' or @class='a3']");
你可以做starts-with
:
$nodes = $xpath->query("//*[@class='abc']//span[starts-with(@class, 'a']");
然而,显然,这也符合apple
或antelope
以及a1
。这可能不是可取的行为。