Jump to content

Failed to open stream: HTTP Request failed.


sogorman

Recommended Posts

I am trying to figure how to code around this. I have a DOM scraper function that pulls urls from my database, opens the page, scrapes the data, and then moves onto the next URL to scrape. my issue is that if the page fails to load the script bombs and I have to restart it again. Trying to figure out how if the

$html->find('div[class="itemHeader address"]') as $div

fails to open the page it just skips the DOM inspection.

 

Here is my error...

 

Failed to open stream: HTTP Request failed.

 

Here is where I am at with my script....

 

mysql_select_db("scraped") or die(mysql_error());
$result = mysql_query("SELECT PKEY, URL, HASSCRAPED, SHOULDSCRAPE FROM CRAWLED WHERE SHOULDSCRAPE ='1' AND HASSCRAPED = '0'") or die(mysql_error());  

while($row = mysql_fetch_array( $result )) {
	mysql_query("UPDATE CRAWLED SET CRAWLED.HASSCRAPED =  '1' WHERE CRAWLED.URL = '" . $row['URL'] . "'");
	$html = file_get_html($row['URL']);
	foreach($html->find('div[class="itemHeader address"]') as $div) {
		foreach($div->find('a') as $element){
			$CleanData = CleanData($element->innertext);
			if (strlen($CleanData[0]) > 5){
   				mysql_query("INSERT INTO SCRAPED (ADDR1, CITY, STATE, ZIP, URL, DATE) VALUES ('" . $CleanData[0] . "','" . $CleanData[1] . "','" . $CleanData[2] . "','" . $CleanData[3] . "','". $row['URL'] . "','". date( 'Y-m-d H:i:s ' ) ."')");
   	   			}
		}
	}

	$html->clear(); 
	unset($html);
	unset($CleanData);
} 

function CleanData($data) {
  $NewData = trim($data);
  $NewData = str_replace("<em>", "", $NewData);
  $AddrCityStateZip = explode("</em>",$NewData);
  $CityStateZip = explode(",",$AddrCityStateZip[1]);
  $StateZip = explode(' ',$CityStateZip[1]);
  $NewDataArray = array ($AddrCityStateZip[0], $CityStateZip[0], $StateZip[1], $StateZip[2]);
  return $NewDataArray;
  unset($NewData);
  unset($AddrCityStateZip);
  unset($CityStateZip);
  unset($StateZip);
}
mysql_close($link);
echo 'Scraping has compleated without error';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.