Buscar links y meter en un array!

Iniciado por A2Corp, 9 Mayo 2010, 22:10 PM

0 Miembros y 1 Visitante están viendo este tema.

A2Corp

Buenas, tengo una gran duda...
Quiero hacer un script que busque los links dentro de una pagina web y escoga alguno y entre.

Tengo este codigo que es para buscar los links


<?
$html = file_get_contents('http://www.example.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);
       $url = $href->getAttribute('href');
       echo $url.',';
}
?>



Y se que con el codigo print_r(explode(',', $url));

los separa en arrays pero solo me muestra uno de los links no todos... no se mucho de php pero creo que es por el "for"
alguien me puede orientar sobre como solucionar esto?
Saludos!
Hackeo, luego existo...

A2Corp

Ya medio complete el codigo pero ahora cuando lo ejecuto se queda cargando...
Que esta mal?
Otra cosa el array sigue agarrando 1 solo valor, no separa todos los links :/
alguna idea?


<?

$html = file_get_contents('http://www.google.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);

       $url = $href->getAttribute('href');

       // echo $url.',';
$separar = explode(",", $url);
$numero = rand(1,10);
}

?>
<script> parent.location.href = "<? echo $separar[$numero];?>"; </script> 
Hackeo, luego existo...

bomba1990

hola, respondiendo a tu pregunta, hace tiempo yo queria hacer lo mismo, y pensando como hiba a hacer hiba a usar la siguiente funcion y listo, preg_match_all, http://php.net/manual/es/function.preg-match-all.php. que todos los resultados te los almacena enun array

tienes que usar expresiones regulares las de perl, y en internet hay bastantes ejemplos de como puede ser la expresion regular para eso. Suerte y avisame como te va.
"Cuando le di de comer a los pobres me llamaron santo, pero cuando pregunte porque los pobres eran pobres me dijeron comunista"

http://sosinformatico.blogspot.com/
http://www.publisnet.com.ve

A2Corp

Q tal bro, gracias por tu interes...
Pues te tengo una novedad, lo logre con un codigo q encontre por ahi, nadamas modifique unas cositas y ya anda bien!
Código (php) [Seleccionar]
<?php
$url 
"http://www.google.com";
    
$var fread_url($url);
            
    
preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+".
                    
"(.*?)[\"\']+.*?>
"."([^<]+|.*?)?<\/a>/",
                    $var, &$matches);
       
    $matches = $matches[1];
    $list = array();





// The fread_url function allows you to get a complete
// page. If CURL is not installed replace the contents with
// a fopen / fget loop

    function fread_url($url,$ref="")
    {
        if(function_exists("curl_init")){
            $ch = curl_init();
            $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
                          "Windows NT 5.0)";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
            curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_REFERER, $ref );
            curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
            $html = curl_exec($ch);
            curl_close($ch);
        }
        else{
            $hfile = fopen($url,"r");
            if($hfile){
                while(!feof($hfile)){
                    $html.=fgets($hfile,1024);
                }
            }
        }
        return $html;
    }
$numero = rand(1,7);
print($matches[$numero]."<br>");
?>
Hackeo, luego existo...