如何获取href属性

时间:2015-11-24 13:05:08

标签: php xpath

我有一个网址: http://www.indeed.com/viewjob?jk=daddefef363643d7&qd=E8dXiB4h7yBMgEwoEDfyDF2ACaqK5NNcKe-lg0a0QeWlgGT7hwsgagao8YFkybxtaLZJqFprtIWhTxIjvWFBLUePVQb0Chqftd-uc7_Pfa4LB2pHYt-YP2NYagtBg9Lp&atk=1a4sk4spi1c0o5la&utm_source=publisher&utm_medium=organic_listings&utm_campaign=affiliate
        我想为视图提取锚的href值并应用
    我的代码是: -

$dom = new DOMDocument();
@$dom->loadHtml($html);
$xpath = new DOMXpath($dom);
$applylink = $xpath->query("//*[@class='job-footer-button-row']/a");
if(!is_null($applylink)){
  $this->view->applylink = $applylink->item(0)->getAttribute('href');
}

但它始终显示以下错误:

 Fatal error: Call to a member function getAttribute() on a non-object

1 个答案:

答案 0 :(得分:0)

这是因为DOMXPath::query在找不到匹配项时不会返回null。请read the documentation查看它返回的内容,这样可以让您更正代码。