<ul id="PrList" class="v2">
<li class="tools">
</li>
<li class="firstRow">
<div class="i">
<a href="www.google.com" title="Google" class="nC">
<img src="something">
</a>
</div>
</li>
</ul>
如何获取 href
<div class="i">
中的属性?
我试过了-
$html = file_get_html($link);
$urls = [];
foreach($html->find('.i') as $element) {
$url = $element->find('.nC')->href;
if (!in_array($url, $urls)) {
echo $url . "<br/>";
$urls[] = $url;
}
}
但我收到一个错误:-
Notice: Trying to get property of non-object
我尝试过:-
$html = file_get_html($link);
$html = $html->find('div.i');
$html -> find('a',0)->href;
$echo $html;
但我再次收到错误:-
Fatal error: Call to a member function find() on array
最佳答案
你需要像下面这样做:-
$html = file_get_html($link);
$urls = [];
foreach($html->find('.i a') as $element) {
$url = $element->href;
if (!in_array($url, $urls)) {
echo $url . "<br/>";
$urls[] = $url;
}
}
echo "<pre/>";print_r($urls);
关于php - 如何使用简单的 HTML DOM 解析器获取 <a> 标签中的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45780576/