Home >Backend Development >PHP Tutorial >Simple code about php crawling page information_PHP tutorial

Simple code about php crawling page information_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:48:38821browse

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();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478432.htmlTechArticleUse php DOM function to achieve simple single-page information capture (here, just capture the a tag, the function is realized , but the extension page link crawling is not implemented, welcome your criticism and guidance) ?php erro...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn