Php 获取href属性
我有以下php代码:Php 获取href属性,php,Php,我有以下php代码: $main_url = "http://www.sports-reference.com/olympics/countries/DEN/summer/1896/"; $main_html=file_get_html($main_url); $link = $main_html->getElementById('div_sports'); foreach ($link->find('td') as $element){ foreac
$main_url = "http://www.sports-reference.com/olympics/countries/DEN/summer/1896/";
$main_html=file_get_html($main_url);
$link = $main_html->getElementById('div_sports');
foreach ($link->find('td') as $element){
foreach($element->find('href') as $node){
echo $node->item(0)->nodeValue . "\n";
//$link_clean = $node->getAttribute('href');
echo $link_clean . "\n";
}
}
如果我打印出$element,我会得到以下输出:
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/ATH/">Athletics</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/FEN/">Fencing</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/GYM/">Gymnastics</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/SHO/">Shooting</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/WLT/">Weightlifting</a></td>
我需要提取以下信息:
/奥运会/国家/丹麦/夏季/1896/ATH/
/奥运会/国家/书斋/夏季/1896年/分/
等等。上面的代码不起作用。你能帮我吗?
href
不是标记,它是标记属性
因此,您必须搜索
:
href
不是标记,而是标记属性
因此,您必须搜索
:
关于stackoverflow,有多个问题。这里有两个:关于stackoverflow,有很多问题。这里有两个:
foreach( $element->find('a') as $a)
{
echo $a->href . "\n";
(...)
}