如何使用简单 HTML DOM 解析器在 <a> 标签中获取 url?
How to get url in <a> tag with Simple HTML DOM Parser?
<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>
如何在 <div class="i">
中获取 href
属性?
我试过了-
$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
试试这个循环。
foreach($html->find('div[class=i] a') as $a){
var_dump($a->attr);
}
你需要像下面那样做:-
$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);
<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>
如何在 <div class="i">
中获取 href
属性?
我试过了-
$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
试试这个循环。
foreach($html->find('div[class=i] a') as $a){
var_dump($a->attr);
}
你需要像下面那样做:-
$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);