Home >Backend Development >PHP Tutorial >Simple code about php crawling page information_PHP tutorial
Use the php DOM function to achieve simple single-page information capture (here we only capture the a tag, the function is implemented, but the extension page link capture is not implemented, welcome your criticism and guidance)
error_reporting(E_ERROR);
$pages = file_get_contents('http://www.php100.com');
//$pages = htmlspecialchars($pages);
$doc = new DOMDocument();
$new_doc = new DOMDocument('1.0', 'utf-8');
$doc->loadhtml($pages);
$dom = $doc->getElementsByTagName('a');
for ($i=0;$i<$dom->length;$i++){
$node = $new_doc->createElement('a',$dom->item($i)->nodeValue);
$newnode = $new_doc->appendChild($node);
$newnode->setAttribute('href',$dom->item($i)->getAttribute('href'));
$newnode->setAttribute('style','display:block;margin-left:30px;');//echo $dom->item($i)->getAttribute('src').'< ;/br>';
}
echo $new_doc->saveHTML();
?>