Playing around with xpath expressions trying to learn it. I found a code snippet, and adjusted it a little. What I’m trying to do is get every link on a page.
$baseurl = "http://www.example.com";
$html = file_get_contents($baseurl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$ahrefs = $xpath->query('//a');
foreach ($ahrefs as $ahref) {
echo $ahref->childNodes->item(0)->nodeValue . "<br />";
}
But now I’m grabbing the anchor text. I want the href part. Maybe even both. What am I doing wrong?
To get the href you have to access the attributes property of the node