0

这是HTML:

<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">Rachel Zoe Expecting Second&nbsp;Child</a></h2>
</div>
<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="someurl" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">sometitle</a></h2>
</div>

我正在尝试获取锚值。这是我的 XPATH:

$finder->query('//div[@class="blogEntry"]//h2//a');

它没有返回任何价值。知道为什么吗?

4

2 回答 2

0

好的!!

这是您要查找的内容:-

//div[contains(@class,"blogEntry")]/h2/a/text()

输出

Rachel Zoe Expecting Second Child
sometitle
于 2013-09-03T20:34:00.717 回答
0

您需要在contains()此处使用该功能:

$xml = <<<EOF
<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">Rachel Zoe Expecting Second&nbsp;Child</a></h2>
</div>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($xml);

$selector = new DOMXPath($doc);

foreach($selector->query('//div[contains(@class,"blogEntry")]/h2/a/text()') as $item) {
    echo $item->nodeValue . PHP_EOL;
}

输出:

Rachel Zoe Expecting Second Child
于 2013-09-03T20:34:13.867 回答