【问题标题】:Getting href attribute获取 href 属性
【发布时间】: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/ ..........

等等。上面的代码不起作用。你能帮我吗?

【问题讨论】:

标签: php


【解决方案1】:

href不是标签,是标签属性。

所以,你必须搜索&lt;a&gt;

foreach( $element->find('a') as $a)
{
    echo $a->href . "\n";
    (...)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 2013-10-03
    • 1970-01-01
    • 2018-06-29
    • 2011-04-18
    • 2011-06-11
    • 2017-05-28
    • 2019-08-18
    相关资源
    最近更新 更多