我有这样的html
<div class="panel panel-info" >
<div class="panel-heading" style="cursor: pointer;">
<img style="height: 10px; width: 20px;" src="images/arrow.gif" alt="arrow" />
<b>
Click Here all regions.
</b>
</div>
<div class="panel-heading" style="cursor: pointer;">
<img style="height: 10px; width: 20px;" src="images/arrow.gif" alt="arrow" />
<b>
Click Here all regions. 2
</b>
</div>
</div>
<div class="panel" >
<div class="panel-heading" style="cursor: pointer;">
<img style="height: 10px; width: 20px;" src="images/arrow.gif" />
<b>
Click Here for3 .
</b>
</div>
</div>
我需要在名为class="panel panel-info"
的特殊类下打印所有b标签值
如此
在php中,我正在尝试这样,但它不起作用
$urlContent = file_get_contents('http://hi.com');
$dom = new DOMDocument();
$dom->loadHtml($urlContent);
$x = new DOMXpath($dom);
$va = $x->query('//div[@class="panel panel-info"]/b');
foreach ($va as $te) {
echo $te->textContent.'</br>';
}
答案 0 :(得分:1)
<?php
$html = '<div class="panel panel-info" >
<div class="panel-heading" style="cursor: pointer;">
<img style="height: 10px; width: 20px;" src="images/arrow.gif" alt="arrow" />
<b>
Click Here all regions.
</b>
</div>
<div class="panel-heading" style="cursor: pointer;">
<img style="height: 10px; width: 20px;" src="images/arrow.gif" alt="arrow" />
<b>
Click Here all regions. 2
</b>
</div>
</div>
<div class="panel" >
<div class="panel-heading" style="cursor: pointer;">
<img style="height: 10px; width: 20px;" src="images/arrow.gif" />
<b>
Click Here for3 .
</b>
</div>
</div>';
$html_dom = new DOMDocument();
@$html_dom->loadHTML($html);
$x_path = new DOMXPath($html_dom);
$nodes= $x_path->query("//div[@class='panel-heading']");
foreach ($nodes as $node)
{
foreach ($x_path->query('b', $node) as $child) {
echo $child->nodeValue, PHP_EOL;
}
}
die;?>
答案 1 :(得分:1)
您的XPath几乎在那儿,问题是当您使用...
Id
//div[@class="panel panel-info"]/b
表示您希望将/b
元素直接放在<b>
元素下面。但是,两者之间还有另一个<div>
元素。解决此问题的最常见方法是使用<div>
,就像您一开始就说要在//
元素下的任意位置使用<b>
标签一样
<div>
答案 2 :(得分:0)
$urlContent = file_get_contents('http://hi.com');
$dom = new DOMDocument();
$dom->loadHtml($urlContent);
foreach ($dom->getElementsByTagName('b') as $te) {
echo $te->nodeValue.'<br>';
}