elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Buscar links y meter en un array!
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Buscar links y meter en un array!  (Leído 3,577 veces)
A2Corp


Desconectado Desconectado

Mensajes: 304



Ver Perfil
Buscar links y meter en un array!
« 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!


En línea

Hackeo, luego existo...
A2Corp


Desconectado Desconectado

Mensajes: 304



Ver Perfil
Re: Buscar links y meter en un array!
« Respuesta #1 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> 


En línea

Hackeo, luego existo...
bomba1990


Desconectado Desconectado

Mensajes: 395



Ver Perfil WWW
Re: Buscar links y meter en un array!
« Respuesta #2 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. 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.
En línea

"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


Desconectado Desconectado

Mensajes: 304



Ver Perfil
Re: Buscar links y meter en un array!
« Respuesta #3 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.  
En línea

Hackeo, luego existo...
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Src-PoC] Buscar en un Array Ordenado
Programación Visual Basic
BlackZeroX 1 2,096 Último mensaje 31 Diciembre 2010, 00:34 am
por Psyke1
Detectar Links y meter en un array « 1 2 »
PHP
Auton 11 5,864 Último mensaje 25 Junio 2011, 17:16 pm
por ~ Yoya ~
Meter en un array la salida de una consulta de varios documentos con Foreach.
PHP
frauzamb 1 1,623 Último mensaje 13 Mayo 2014, 20:02 pm
por DanB
como meter datos en un array
Programación C/C++
alexmila 2 1,766 Último mensaje 18 Mayo 2017, 20:12 pm
por MAFUS
como puedo buscar links?
Dudas Generales
tomatito52 1 2,514 Último mensaje 5 Febrero 2022, 22:20 pm
por El_Andaluz
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines