Extracción de URL de imagen de otra página web

  • Autor Autor newmesis
  • Fecha de inicio Fecha de inicio
N

newmesis

Beta
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Hola
e tratado con algunos codig y e podido hacerlo pero no como yo quiero


en este codigo puedo extraer las imagenes

PHP:
function getImage($url) { // GET IMDB image
    try { 
	$doc = new DOMDocument();
	$doc->loadHTML( file_get_contents($url) );
	$xpath = new DOMXPath($doc);
	return $xpath->query('//div[@class="bg-movie alpha omega"]/img')->item(0)->getAttribute('src');
    } catch( Exception $e ) { return null; }
}
 
echo getImage("http://www.cinemundo.cl/peliculas/batman-el-caballero-de-la-noche-asciende/");

y resulta bien
y con este codigo

obtengo el rss actualizado de la pagina la cual no trae la url de la imagen

PHP:
class RssReader { 
	    var $url; 
	    var $data; 
	    
	    function RssReader ($url){ 
	        $this->url; 
	        $this->data = implode ("", file ($url)); 
	    } 
	    
	    function get_items (){ 
	        preg_match_all ("/<item .*>.*<\/item>/xsmUi", $this->data, $matches); 
	        $items = array (); 
	        foreach ($matches[0] as $match){ 
	            $items[] = new RssItem ($match); 
	        } 
	        return $items; 
	    } 
	} 
	
	class RssItem { 
	    var $title, $url, $description; 
	    
	    function RssItem ($xml){ 
	        $this->populate ($xml); 
	    } 
	    
	    function populate ($xml){ 
	        preg_match ("/<title> (.*) <\/title>/xsmUi", $xml, $matches); 
	        $this->title = $matches[1]; 
	        preg_match ("/<link> (.*) <\/link>/xsmUi", $xml, $matches); 
	        $this->url = $matches[1]; 
	        preg_match ("/<description> (.*) <\/description>/xsmUi", $xml, $matches); 
	        $this->description = $matches[1]; 
    } 
	    
    function get_title (){ 
        return iconv('UTF-8', 'ISO-8859-1', $this->title);
	    } 
	
	    function get_url (){ 
        return iconv('UTF-8', 'ISO-8859-1', $this->url); 
	    } 
	    
	    function get_description (){ 
	        return iconv('UTF-8', 'ISO-8859-1', $this->description); 
	    } 
	} 


//Ejemplo de como usarlo
$rss = new RssReader ("http://www.cinemundo.cl/cines/roble/feed/"); 
	$hola = 'hola';
	foreach ($rss->get_items () as $item){ 
		printf ('<div><a href="%s">%s</a><br />%s<br /><br />',
		html_entity_decode($item->get_url ()),
		html_entity_decode($item->get_description ()),
		html_entity_decode($item->get_title ())); 
		printf ('</div>');
		 
}
 
echo hola ($link) ;}

lo que e tratado de hacer es de fucionar el codigo queando algo asi

PHP:
class RssReader { 
	    var $url; 
	    var $data; 
	    
	    function RssReader ($url){ 
	        $this->url; 
	        $this->data = implode ("", file ($url)); 
	    } 
	    
	    function get_items (){ 
	        preg_match_all ("/<item .*>.*<\/item>/xsmUi", $this->data, $matches); 
	        $items = array (); 
	        foreach ($matches[0] as $match){ 
	            $items[] = new RssItem ($match); 
	        } 
	        return $items; 
	    } 
	} 
	
	class RssItem { 
	    var $title, $url, $description; 
	    
	    function RssItem ($xml){ 
	        $this->populate ($xml); 
	    } 
	    
	    function populate ($xml){ 
	        preg_match ("/<title> (.*) <\/title>/xsmUi", $xml, $matches); 
	        $this->title = $matches[1]; 
	        preg_match ("/<link> (.*) <\/link>/xsmUi", $xml, $matches); 
	        $this->url = $matches[1]; 
	        preg_match ("/<description> (.*) <\/description>/xsmUi", $xml, $matches); 
	        $this->description = $matches[1]; 
    } 
	    
    function get_title (){ 
        return iconv('UTF-8', 'ISO-8859-1', $this->title);
	    } 
	
	    function get_url (){ 
        return iconv('UTF-8', 'ISO-8859-1', $this->url); 
	    } 
	    
	    function get_description (){ 
	        return iconv('UTF-8', 'ISO-8859-1', $this->description); 
	    } 
	} 


//Ejemplo de como usarlo
$rss = new RssReader ("http://www.cinemundo.cl/cines/roble/feed/"); 
	$hola = 'hola';
	foreach ($rss->get_items () as $item){ 
		printf ('<div><a href="%s">%s</a><br />%s<br /><br />',
		html_entity_decode($item->get_url ()),
		html_entity_decode($item->get_description ()),
		html_entity_decode($item->get_title ())); 
		printf ('</div>');
		 $link = html_entity_decode($item->get_url ());
		 
	
		 $a= $a + 1;
		 if($a == 1){
function hola ($url) { // GET IMDB image
    try { 
	$doc = new DOMDocument();
	$doc->loadHTML( file_get_contents($url) );
	$xpath = new DOMXPath($doc);
	return $xpath->query('//div[@class="bg-movie alpha omega"]/img')->item(0)->getAttribute('src');
    } catch( Exception $e ) { return null; }
}
      hola ;
echo hola ("http://www.cinemundo.cl/peliculas/batman-el-caballero-de-la-noche-asciende/") ;}
	   
 					 if($a == 2){
function hola2 ($url) { // GET IMDB image
    try { 
	$doc = new DOMDocument();
	$doc->loadHTML( file_get_contents($url) );
	$xpath = new DOMXPath($doc);
	return $xpath->query('//div[@class="bg-movie alpha omega"]/img')->item(0)->getAttribute('src');
    } catch( Exception $e ) { return null; }
}
 
echo hola ($link) ;}

	}

pero no consigo que funcione
 
Mmmm es un dolor de ojos ese codigo XD
Pusiste las funciones en el bucle del foreach (?)
Y encima las dos funciones (hola y hola2) hacen lo mismo 😵

Acá te lo dejo andando:
PHP:
<?php
class RssReader {
        var $url;
        var $data;

        function RssReader ($url){
            $this->url;
            $this->data = implode ("", file ($url));
        }

        function get_items (){
            preg_match_all ("/<item .*>.*<\/item>/xsmUi", $this->data, $matches);
            $items = array ();
            foreach ($matches[0] as $match){
                $items[] = new RssItem ($match);
            }
            return $items;
        }
    }

    class RssItem {
        var $title, $url, $description;

        function RssItem ($xml){
            $this->populate ($xml);
        }

        function populate ($xml){
            preg_match ("/<title> (.*) <\/title>/xsmUi", $xml, $matches);
            $this->title = $matches[1];
            preg_match ("/<link> (.*) <\/link>/xsmUi", $xml, $matches);
            $this->url = $matches[1];
            preg_match ("/<description> (.*) <\/description>/xsmUi", $xml, $matches);
            $this->description = $matches[1];
    }

    function get_title (){
        return iconv('UTF-8', 'ISO-8859-1', $this->title);
        }

        function get_url (){
        return iconv('UTF-8', 'ISO-8859-1', $this->url);
        }

        function get_description (){
            return iconv('UTF-8', 'ISO-8859-1', $this->description);
        }
    }


//Ejemplo de como usarlo
function hola ($url) { // GET IMDB image
    try{
        $doc = new DOMDocument();
        $doc->loadHTML( file_get_contents($url) );
        $xpath = new DOMXPath($doc);
        $res = $xpath->query('//div[@class="bg-movie alpha omega"]/img')->item(0);

        return ($res ? $res->getAttribute('src') : 'url de una imagen por defecto.jpg');

    } catch( Exception $e ) {  }
}

$rss = new RssReader ("http://www.cinemundo.cl/cines/roble/feed/");

foreach ($rss->get_items () as $item){
    echo sprintf('<div><a href="%s">%s</a><br />%s<br /><br /><img src="%s" alt="" /></div>',
    html_entity_decode($item->url),
    html_entity_decode($item->title),
    html_entity_decode($item->description),
    hola($item->url));
}
 
gracias por responder,

pero el codigo tuyo solo me entrega imagen por defecto
con un amigo tratamos de hacer el codigo pero aun asi no podemos llegar a concretar

PHP:
	class RssReader { 
	    var $url; 
	    var $data; 
	    
	    function RssReader ($url){ 
	        $this->url; 
	        $this->data = implode ("", file ($url)); 
	    } 
	    
	    function get_items (){ 
	        preg_match_all ("/<item .*>.*<\/item>/xsmUi", $this->data, $matches); 
	        $items = array (); 
	        foreach ($matches[0] as $match){ 
	            $items[] = new RssItem ($match); 
	        } 
	        return $items; 
	    } 
	} 
	
	class RssItem { 
	    var $title, $url, $description; 
	    
	    function RssItem ($xml){ 
	        $this->populate ($xml); 
	    } 
	    
	    function populate ($xml){ 
	        preg_match ("/<title> (.*) <\/title>/xsmUi", $xml, $matches); 
	        $this->title = $matches[1]; 
	        preg_match ("/<link> (.*) <\/link>/xsmUi", $xml, $matches); 
	        $this->url = $matches[1]; 
	        preg_match ("/<description> (.*) <\/description>/xsmUi", $xml, $matches); 
	        $this->description = $matches[1]; 
    } 
	    
    function get_title (){ 
        return iconv('UTF-8', 'ISO-8859-1', $this->title);
	    } 
	
	    function get_url (){ 
        return iconv('UTF-8', 'ISO-8859-1', $this->url); 
	    } 
	    
	    function get_description (){ 
	        return iconv('UTF-8', 'ISO-8859-1', $this->description); 
	    } 
	} 


//Ejemplo de como usarlo
$rss = new RssReader ("http://www.cinemundo.cl/cines/roble/feed/"); 
	
	foreach ($rss->get_items () as $item){ 
		printf ('<div><a href="%s">%s</a><br />%s<br /><br />',
		html_entity_decode($item->get_url ()),
		html_entity_decode($item->get_description ()),
		html_entity_decode($item->get_title ())); 
		printf ('</div>');
		
        $url = html_entity_decode($item->get_url ());
		
		$doc = new DOMDocument();
		$doc->loadHTMLFile($url);
		$xpath = new DOMXpath($doc);
		$result = $xpath->query('//div[@class="bg-movie alpha omega"]/img/@src');

		foreach ($result as $srcAttr) {
    	echo $srcAttr->value, "\n";
		}

 

	} 
	
	

?>
 
Muestra imagen por defecto, porque las direcciones del feed que estan usando no usan imagenes....
aca hay un ejemplo:
Enlace eliminado
 
Muestra imagen por defecto, porque las direcciones del feed que estan usando no usan imagenes....
aca hay un ejemplo:
Enlace eliminado

muchas gracias ahora me di cuenta el error . ahora

claramente el rss me da mal la url para sacar la imagen pero si tengo claro que url esta dando ejemplo

la url que da el rss es algo asi

Enlace eliminado

y la url real donde se encuentra la imagen es asi

Enlace eliminado

osea agrega el -chillan/

como puedo hacer para sacarle esto -chillan/
para asi dejar la url limpia

te agradesco por responder
 
En la penúltima linea del código que te pase:
PHP:
hola(str_replace('-chillan', '', $item->url)));
 
Atrás
Arriba