简体   繁体   中英

How to get the href attribute

I have a url: 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
I want to extract href value of anchor for view and apply
my code is:-

$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');
}

But it always shows below error:

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

This happens because DOMXPath::query does not return null when it finds no matches. Please read the documentation to see what it returns, and that should allow you to correct your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM