【发布时间】:2016-06-07 12:12:46
【问题描述】:
我有这个 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){
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>
我需要提取此信息:
/olympics/countries/DEN/summer/1896/ATH/ /奥运会/国家/DEN/summer/1896/FEN/ ..........
等等。上面的代码不起作用。你能帮我吗?
【问题讨论】:
-
stackoverflow 上有多个关于此的问题。这里有两个:stackoverflow.com/questions/4702987/…stackoverflow.com/questions/6365701/php-extract-link-from-a-tag
标签: php