I'm trying to scrape a specific div with an id on a page
domdocument, php, web-scraping
Solution
You can use this:
/**
* Downloads a web page from $url, selects the the element by $id
* and returns it's xml string representation.
*/
function getElementByIdAsString($url, $id, $pretty = true) {
$doc = new DOMDocument();
@$doc->loadHTMLFile($url);
if(!$doc) {
throw new Exception("Failed to load $url");
}
// Obtain the element
$element = $doc->getElementById($id);
if(!$element) {
throw new Exception("An element with id $id was not found");
}
if($pretty) {
$doc->formatOutput = true;
}
// Return the string representation of the element
return $doc->saveXML($element);
}
// call it:
echo getElementByIdAsString('http://www.google.com', 'lga');
Problem
I want to scrape the contents of a page, well really just a single div from that page, and display it to the user inside of a small div on a webpage. I just need a piece of info from a carfax page that needs user credentials so I can't post the exact code but I tried using google.com and have the same problem so the solution should cross over. Right now I've tried this: ``` $webPage = file_get_contents('http://www.google.com'); $doc = new DOMDocument(); $doc->loadHTML($webPage); $div = $doc->getElementById('lga');//this is the id to the div holding the image above the textbox //echo $webPage;//this displays www.google.com minus the image. I imagine because of the file path //var_dump($div);//this display "object(DOMElement)#2 (0) { }" and I'm not sure what that means //echo $div;//this has a server error ``` I'm also looking at simple_html_dom.php trying to figure that out.