So I am trying to make/remake a Web Scraper in PHP using DOMDocument. The project I have to complete needs to be in native PHP, so no using cURL. I looked at using reg expressions but DOMDocument seems better.
Anyways I cannot get it to output and I am not sure why. Am I not calling something forward right?
<?php
class WebScraper{
private $url = 'http://todaysinfo.net/top-15-most-dangerous-airports/?utm_source=outbrain_airports&utm_campaign=outbrain_airports';
private $elements = array('title', 'p', 'img');
private $scraper_doc = null;
public function __construct($url){
if($url){
$this->url = $url;
$this->scrapeData();
if($this->scraper_doc){
$this->parseData();
$this->outPut();
} else {
echo '<p style="color: red;">Something happened with DOMDocument."';
}
}
}
function scrapeData(){
$urlContents = @file_get_contents($this->$url);
if($urlContents){
$this->scraper_doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
$this->scraper_doc->loadHTML($urlContents);
} else {
echo '<p style="color: red;">Didn\'t grab all of the contents."';
}
}
function parseData(){
foreach($this->$elements as $element){
$scraper_row = $this->scraper_doc->getElementsByTagName($element);
foreach($scraper_row as $row){
if($element == 'img'){
echo $row->getAttribute('src') . "<br />";
} else {
echo $row->nodeValue . "<br />";
}
}
}
}
}
?>