简体   繁体   中英

How to get all the <a> tags from the <body> tag using PHP DOM?

I am using PHP's curl for getting webpage data, and for extracting <a> tags from the <body> I am using DOM Document, but it is creating an error.

<?php
$ch = curl_init();
curl_setopt_array($ch, array(
  CURLOPT_URL => "http://www.google.co.in/?gfe_rd=cr&ei=B5GBVezbDeHA8geU8pfYBw",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_USERAGENT => 'Webbot UA'
));
$result = curl_exec($ch);
curl_close($ch);
if (isset($result)){
  $doc = new DomDocument;
  $doc->Load($result);
  var_dump($doc['a']);
}
?>

I would not use DomDocument, use SimpleXMLElement::xpath but that's just because I believe it's faster in execution, may be wrong though.

$result = $xml->xpath('//a');
while(list( , $node) = each($result)) {
    echo 'a: ',$node,"\n";
}

To use DomDocument look at DOMDocument::getElementsByTagName

$books = $dom->getElementsByTagName('a');
foreach ($books as $book) {
    echo $book->nodeValue, PHP_EOL;
}

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