Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: A2Corp en 9 Mayo 2010, 22:10 pm



Título: Buscar links y meter en un array!
Publicado por: A2Corp en 9 Mayo 2010, 22:10 pm
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

Código:
<?
$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
Código:
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!


Título: Re: Buscar links y meter en un array!
Publicado por: A2Corp en 10 Mayo 2010, 08:04 am
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?

Código:
<?

$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> 


Título: Re: Buscar links y meter en un array!
Publicado por: bomba1990 en 11 Mayo 2010, 04:44 am
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 (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.


Título: Re: Buscar links y meter en un array!
Publicado por: A2Corp en 11 Mayo 2010, 06:03 am
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
  1. <?php
  2. $url = "http://www.google.com";
  3.    $var = fread_url($url);
  4.  
  5.    preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+".
  6.                    "(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/",
  7.                    $var, &$matches);
  8.  
  9.    $matches = $matches[1];
  10.    $list = array();
  11.  
  12.  
  13.  
  14.  
  15.  
  16. // The fread_url function allows you to get a complete
  17. // page. If CURL is not installed replace the contents with
  18. // a fopen / fget loop
  19.  
  20.    function fread_url($url,$ref="")
  21.    {
  22.        if(function_exists("curl_init")){
  23.            $ch = curl_init();
  24.            $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
  25.                          "Windows NT 5.0)";
  26.            $ch = curl_init();
  27.            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  28.            curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
  29.            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  30.            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
  31.            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
  32.            curl_setopt( $ch, CURLOPT_URL, $url );
  33.            curl_setopt( $ch, CURLOPT_REFERER, $ref );
  34.            curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  35.            $html = curl_exec($ch);
  36.            curl_close($ch);
  37.        }
  38.        else{
  39.            $hfile = fopen($url,"r");
  40.            if($hfile){
  41.                while(!feof($hfile)){
  42.                    $html.=fgets($hfile,1024);
  43.                }
  44.            }
  45.        }
  46.        return $html;
  47.    }
  48. $numero = rand(1,7);
  49. print($matches[$numero]."<br>");
  50. ?>
  51.