Search code examples
phphtmldomscraper

Converting html to url scraper


So a very helpful guy as helped me get this far on Stackoverflow however I need to covert his code from HTMl to a URL to scrape I've tried over and over and I keep hitting errors any ideas?

function getElementByIdAsString($html, $id, $pretty = true) {
$doc = new DOMDocument();
@$doc->loadHTML($html);

if(!$doc) {
    throw new Exception("Failed to load $url");
}
$element = $doc->getElementById($id);
if(!$element) {
    throw new Exception("An element with id $id was not found");
}

// get all object tags
$objects = $element->getElementsByTagName('object'); // return node list

// take the the value of the data attribute from the first object tag
$data = $objects->item(0)->getAttributeNode('data')->value;

// cut away the unnecessary parts and return the info
return substr($data, strpos($data, '=')+1);

}

// call it:
$finalcontent = getElementByIdAsString($html, 'mainclass');

print_r ($finalcontent);

Solution

  • Remember to try and catch when you use your function as it is likely to throw Exceptions which will cause a 500 Server error.

    $finalcontent = getElementByIdAsString($html, 'mainclass');

    should become

    try {
        $finalcontent = getElementByIdAsString($html, 'mainclass');
    }catch(Exception $e){
        echo $e->getMessage();
    }