curl - How can I echo a scraped div in PHP? -
how echo , scrape div class? tried doesn't work. using curl establish connection. how echo it? want how on actual page. $document = new domdocument(); $document->loadhtml($html); $selector = new domxpath($document); $anchors = $selector->query("/html/body//div[@class='resultitem']"); //a url want retrieve
foreach($anchors $a) { echo $a; }
neighbor, made snippet below, uses logic, , tweaks display specified class webpage in get_contents function. maybe can plug in values , try it?
(note: put error checking in there see few bugs. can helpful use tweak. )
<?php error_reporting(e_all); ini_set('display_errors', '1'); $url = "http://www.tizag.com/csst/cssid.php"; $class_to_scrape="display"; $html = file_get_contents($url); $document = new domdocument(); $document->loadhtml($html); $selector = new domxpath($document); $anchors = $selector->query("/html/body//div[@class='". $class_to_scrape ."']"); echo "ok, no php syntax errors. <br>lets see scraped.<br>"; foreach ($anchors $node) { $full_content = innerhtml($node); echo "<br>".$full_content."<br>" ; } /* function preserves inner content of scraped element. ** http://stackoverflow.com/questions/5349310/how-to-scrape-web-page-data-without-losing-tags ** sure go , give post uptick too:) **/ function innerhtml(domnode $node) { $doc = new domdocument(); foreach ($node->childnodes $child) { $doc->appendchild($doc->importnode($child, true)); } return $doc->savehtml(); } ?>
Comments
Post a Comment