【问题标题】:get the href attribute from the webpage?从网页获取 href 属性?
【发布时间】:2012-08-27 09:44:21
【问题描述】:

我需要从以下获取 href 属性:

<tr>
    <td><h2 class="officers"><a href="/finance/stocks/officerProfile?symbol=ABB.N&officerId=232795" class="link">Roger&nbsp;Agnelli</a></h2></td>
    <td>53</td>
    <td>2002</td>
    <td>Non-Executive Member of the Board of Directors</td>
</tr>

我在这里尝试的是

$a = $tr->getElementsByTagName('a');

echo $a->getAttribute('href');

无法获取 href 值。我在哪里失踪??我在这里需要什么,我希望输出 href 链接,然后将该 href 链接解析为“officer id”。

希望我的问题很清楚..帮助我..

【问题讨论】:

  • 你会得到什么?
  • 未定义方法 DOMNodeList::getAttribute()

标签: php parsing html-parsing


【解决方案1】:

getAttribute 不是DOMNodeList 的方法,它是DOMElement 的方法。所以你必须做到以下几点:

foreach ($a as $element){
    var_dump($element->getAttribute('href'));
}

而不是echo $a-&gt;getAttribute('href');

【讨论】:

    猜你喜欢
    • 2022-11-28
    • 2016-06-07
    • 2017-05-28
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多