我有一个看起来像这样的html页面
xxxx
<a href="www.google.com">google!</a>
<div class="big-div">
<a href="http://www.url.com/123" title="123">
<div class="little-div">xxx</div></a>
<a href="http://www.url.com/456" title="456">
<div class="little-div">xxx</div></a>
</div>
xxxx
Run Code Online (Sandbox Code Playgroud)
我正在尝试将href排除在big-div外。我可以使用以下代码获取整个页面中所有的href。
$links = $html->find ('a');
foreach ($links as $link)
{
echo $link->href.'<br>';
}
Run Code Online (Sandbox Code Playgroud)
但是,如何在div“ big-div”中仅获取href的内容。编辑:我想我明白了。对于那些关心:
foreach ($html->find('div[class=big-div]') as $element) {
$links = $element->find('a');
foreach ($links as $link) {
echo $link->href.'<br>';
}
}
Run Code Online (Sandbox Code Playgroud)