Clase Spider para obtener metas y contenido sin html tags de una web

28 28UTC Agosto 28UTCJueves 2008 at 11:38 pm (PHP)

Hola, estaba trabajando en un pequeño buscador de sitios webs, y me tope con el problema de que no habia ninguna script por ahi que limpie correctamente una web de los html tags para obtener solo el codigo, los meta y el titulo. Por ello me puse a escribir una clase que haga estas cosas.

Quedaron algunas pequeñas funcionalidades por crear, pueden verse en los comentarios de la script, como arreglar acentos y usar fsockopen si no hay libreria curl, tambien algunas funcionalidades un poco mas importantes como obtener todos los alts de las imagenes y obtener todas las links. No obstante creo que funcionara de maravillas para lo que necesito.

Ejemplo de uso;

$webFetch=Spider::getWebFull('http://technorati.com/');
print_r($webFetch);

//@author Eugenio Fage
abstract class Spider {
	public function getWebFull($url){
		$htmlCode=self::getWebCode($url);
		if($htmlCode=='') return array();
		$return['title']=self::getTitle($htmlCode);
		$return['metas']=self::getMetas($htmlCode);

		$return['text']=self::justText($htmlCode);

		return $return;
	}

	public function getWebCode($url){
		//@todo si no existen las curl functions usar fsockopen
		$ch = curl_init();

		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		$data = curl_exec ($ch);
		curl_close ($ch);

		return $data;
	}

	public function getTitle($html,$charset=null){
		//@todo corregir acentos sin usar multi byte functions
		$arr=array();
		preg_match_all('@(<title>(.*)</title>)@i',$html,$arr);
		$arr=$arr[2];
		//el titulo no va a ser mas largoque 100 caracteres
		return(substr(strip_tags($arr[0]),0,110));
	}

	public function getMetas($html,$charset=null){
		//@todo corregir acentos sin usar multi byte functions
		$arr=array();
		preg_match_all('@(meta\sname=\"(.*)\"\scontent=\"(.*)\"[ /]*>)@i',$html,$arr);
		$meta=$arr[2];
		$content=$arr[3];
		unset($arr);

		while(($unMeta=array_pop($meta))){
			$metas[strtolower($unMeta)]=array_pop($content);
		}

		while(($unMeta=array_pop($meta))){
			$metas[strtolower($unMeta)]=array_pop($content);
		}

		preg_match_all('@(meta\scontent=\"(.*)\"\sname=\"(.*)\"[ /]*>)@i',$html,$arr);
		$meta=$arr[3];
		$content=$arr[2];
		unset($arr);

		while(($unMeta=array_pop($meta))){
			$metas[strtolower($unMeta)]=array_pop($content);
		}

		return $metas;
	}

	public function justText($html,$charset=null){
		//@todo corregir acentos sin usar multi byte functions
		$html=str_replace('>','> ', $html);

		$buscar=array('@<!--.*?-->@si','@<script[^>]*?>.*?</script>@si','@<style[^>]*?>.*?</style>@si');
		$html = preg_replace($buscar, ' ', $html);

		$html = preg_replace('@<.*?>@si', ' ', $html);

		$html=str_replace('&lt;',' ',$html);
		$html=str_replace('&gt;',' ',$html);

		$html=html_entity_decode(strip_tags($html));

		$html=str_replace(array('<','>','&gt;','&lt;',"\t",chr(13),chr(10),chr(160)),' ',$html);

		while(strpos($html,'  ')!==false){
			$html=str_replace('  ',' ',$html);
		}

		return substr($html,0,1500);
	}

}

1 comentario

  1. nAcho dijo:

    Interesante

Escribe un comentario