Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] estructura de una web para parsear

Estas en el tema de estructura de una web para parsear en el foro de PHP en Foros del Web. Hola chicos/as, en mi aprendizaje he descubierto el mundo de parsear. Tengo una duda a ver si me podéis echar una mano. Según veo la ...
  #1 (permalink)  
Antiguo 07/01/2015, 16:24
 
Fecha de Ingreso: diciembre-2013
Mensajes: 37
Antigüedad: 10 años, 4 meses
Puntos: 1
estructura de una web para parsear

Hola chicos/as, en mi aprendizaje he descubierto el mundo de parsear. Tengo una duda a ver si me podéis echar una mano.

Según veo la información de una web se puede acceder por json, xml, rss, html, yaml, web service, tags, div.

Puede que haya dicho alguna burrada, porque tengo los conceptos sin aprender todavía y por eso os pregunto.

Si quiero poner una búsqueda en google de bizcochos, por ejemplo, y parsear los enlaces que me devuelva. ¿Cómo lo haría?

Lo que se me ocurrió es meter todo en un string e ir buscando los enlaces con una función, pero veo que hay formas más fáciles/efectivas. Cómo sé si una web usa xml, json, etc para parsear? ¿Viendo el código fuente?
Estoy echa un lío con esto, me parece super difícil y a poco que busco me salen más dudas. ¿Alguien tiene una explicación para tontas a modo de resumen?
  #2 (permalink)  
Antiguo 07/01/2015, 20:00
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: estructura de una web para parsear

Puedes llamar la pagina con curl y después el resultado lo pasas a el DOM
Y con el dom yapuedes hacerlo que quieras
  #3 (permalink)  
Antiguo 09/01/2015, 08:57
 
Fecha de Ingreso: diciembre-2013
Mensajes: 37
Antigüedad: 10 años, 4 meses
Puntos: 1
Respuesta: estructura de una web para parsear

Cita:
Iniciado por enlinea777 Ver Mensaje
Puedes llamar la pagina con curl y después el resultado lo pasas a el DOM
Y con el dom yapuedes hacerlo que quieras
Gracias por la contestación, ¿algún ejemplo de cómo hacerlo?
No se muy bien cómo hacerlo.

De momento estoy haciendo una función para buscar los enlaces, pero no obtengo los resultados que quiero. Cuando la termine la pondré por aquí para que le echéis un ojo.
  #4 (permalink)  
Antiguo 11/01/2015, 04:03
 
Fecha de Ingreso: diciembre-2013
Mensajes: 37
Antigüedad: 10 años, 4 meses
Puntos: 1
Respuesta: estructura de una web para parsear

Actualizo que he terminado por fin una función para recuperar los enlaces desde php. Me ha costado porque tenía errores y no sabía porqué.

Ahora lo que voy a hacer es mirar lo de curl, por qué si ya tengo una función que lo hace?

Pues porque la función file_get_contents es muy lenta para descargar una web. En las pruebas que he hecho con curl y file_get_contents, gana curl con diferencia, además, sin saber por qué, a veces file_get_contents tarda más de 1 segundo en descargar la web en un string, cuando lo normal es que sean 0.3-0.4 segundos.

En fin, a seguir investigando. Un besito a tod@s
  #5 (permalink)  
Antiguo 12/01/2015, 07:25
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: estructura de una web para parsear

http://simplehtmldom.sourceforge.net/
Esto te puede ayudar bastante.
  #6 (permalink)  
Antiguo 12/01/2015, 11:21
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 10 meses
Puntos: 127
Respuesta: estructura de una web para parsear

Hola Andreiya

aqui te dejo el codigo para parsear en google, espero te ayude.

aqui el ejemplo en vivo

http://creaelicita.cl/ayuda/foro_111...hp?texto=gatos

usa la variable texto para buscar lo que quieras

Código PHP:
<?php
include('simple_html_dom.php'); 
  
        
$handler curl_init();// iniciar el comando CURL POR PHP
        
curl_setopt($handlerCURLOPT_URL'https://www.google.com/search?q=gatitos&ie=utf-8&oe=utf-8');
        
curl_setopt($handlerCURLOPT_RETURNTRANSFER1);
        
curl_setopt($handler,CURLOPT_USERAGENT,'Mozilla/4.0 (PDA; PalmOS/sony/model prmr/Revision:1.1.54 (en)) NetFront/3.0');// IMPORTANTE HACRCE PASAR POR UNA PALM TE ASEGURA UN RESULTADO MAS LIMPIO
        // OTROS COMPLEMENTOS
        
curl_setopt($handler,CURLOPT_HOTS,'www.google.com');
        
curl_setopt($handler,CURLOPT_ACCEPT,'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
        
curl_setopt($handler,CURLOPT_ACCEPTLANGUAGE,'es-cl,es;q=0.8,en-us;q=0.5,en;q=0.3');
        
curl_setopt($handler,CURLOPT_COOKIE,'NID=67=nHypWZWZS_T-SlSSuXAJTMhZB4gIvYOtOUP0vjoOPGjE8HYSK0ONSY1luZeWkIvH7iqpVdxH6Xf0BEcTqIj1Y6rrZJQ8z8O3HdX2gFekYHs-Eh0dJ6d_aw8Lq2VHtFwlBLVxEk5P4t77cyvTNK7EeEDrmUtrgoBFEX72EDgPHvJ42GJshGlaYLTa-Gr2pguDypLsCNRySdDN5dyxSnIn; SID=DQAAAAICAABeps3N4B3sU4HJ1vIE97a98emN7n94wpIoW5MJRRMNHexTSzoY_NfXTV8CIZ2NW6vnC0K2G8Sq7YqEWI_pEEuHPvWA4CkLxMFMC_An3BkmgX9ZD9MywaLuUGlMF-3Hyf8PgJbc6NYTqaN1Qo6duT3aMplkRVmVMHVdPfq46VffGIq0ayEzcd-uYmTjE7ckT0rpb-RqrcuGUyvqUZPnvFbCwDqcGtSd98bEj6bfwDyG3Ap6apF_v6DmQan-eNkIE1FEXzHB0bXd_nKROCm4FKJrz1tONYJGRVP43XHbgv89UkEyF9OAgiZvUB3PyFUU4ihkv0raYQZBcLiiY6NvRPANpKfN885MNwaFPHLHR1woTELjrorj-YASvVxr8ai3blqs7nFI8fy1S_DwWe_weJxn6PvZsX757xh0GQBMOfKznHlx6XFNKaVmwlhdhwlVY3UfJO-svRhpOSCReeLzFlhye19A1sSY-HCTlBEZVI6eXKhCkb5YzTr5DZM79bC2Px45RBu39M4i8znhjIgNe3a6xC5P6crGjAkes6qx7LEdc9keJB27X0ZN5e_S6wccrp9zbMGn9jJ03hW47A4FISDqhyETipj40sXIx0xEDungKl9U_4cGbAMeyWdjwc-HqVu4jSHNw5mOwNloTY_zFWlAdXnalvNzRoBEiRWHHh59Z9rtvxnekr33q7t3xMqo8Gg; HSID=AbEEV6EbfNcqLumK2; SSID=ApgzPR1FFU_d2zQU7; APISID=HugUvpV0VR3gJMIQ/AVyBODG-BvQpt5r7O; SAPISID=oCYqAoqsB6_lPdrh/AFw8b_LEX6RubiuAr; PREF=ID=b904718712cc0d24:U=39272d8d77cdf325:FF=0:LD=es-419:NR=100:TM=1407341101:LM=1421081382:GM=1:SG=1:S=Iy_OMhncF0Buyype; GOOGAPPUID=739; enabledapps.uploader=0; llbcs=0');
        
curl_setopt($ch,CURLOPT_CONNECTION,'keep-alive');
        
curl_setopt($ch,CURLOPT_PRAGMA,'no-cache');
        
curl_setopt($ch,CURLOPT_CACHECONTROL,'no-cache');
    
$response curl_exec($handler);// EJECURAT Y DEVOLVER EL RESULTADO
    
    
curl_close($handler); //CERRAR LA LLAMADA
    
    // ALGUNOS FILTROS  PARA EL HTML  
    
$response ereg_replace("<script([^>]*)>([^<]*)<\/script>",'',$response);
    
$response preg_replace('/(<[^>]+) style=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) class=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) bgcolor=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) border=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) title=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) onDblClick=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) onClick=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) class=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) role=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) tabindex=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) aria-expanded=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) aria-haspopup=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) data-ved=".*?"/i''$1'$response);
    
$response preg_replace('/\<ol\>/i''$1'$response);$response preg_replace('/\<\/ol\>/i''$1'$response);
    
$response preg_replace('/\<h.\>/i''$1'$response);$response preg_replace('/\<\/h.\>/i''$1'$response);
    
$response preg_replace('/\<br\>/i''$1'$response);
    
$response preg_replace('/\<span\>/i''$1'$response);$response preg_replace('/\<\/span\>/i''$1'$response);
    
$response preg_replace('/\<ul\>/i''$1'$response);    $response preg_replace('/\<\/ul\>/i''$1'$response);
    
$response preg_replace('/\<b\>/i''$1'$response);    $response preg_replace('/\<\/b\>/i''$1'$response);
    
$response ereg_replace("<cite([^>]*)>([^<]*)<\/cite>",'',$response);
    
$response =preg_replace('/thead/''tr'$response);
    
$response =preg_replace('/\<\/th>/''</td> '$response);
    
$response preg_replace('/tbody/''tr'$response);

?><h1>Links</h1><?      
$html 
str_get_html($response);  // USAR  simple_html_dom   PARA FACILITAR LA BUSQUEDA
$ret $html->find('a'); // BUSCAR LOS LINKS

    
foreach($ret as $element){// RECORRER LOS LINKS
        
$link=$element->href;
        
$link='http'.preg_replace('/http(.*)http/'''$link);// REEMPLASA LOS LINKS DE GOOGLE
        
$texto=$element->plaintext;// TEXTO DEL LINK
        
$q=substr($link,0,5);// LOS PRIMEROS 5 CARACTERES PARA FILTRAR RESULTADOS ENTRE PAGINAS DE GOOGLE Y BUSQIEDAS
        
        
if($q=='http:' && $texto!='Cached'){// FILTRADO ENTRE PAGINAS DE GOOGLE Y BUSQIEDAS
        
?>
        <a href="<?=$link?>"><?=$texto?$texto:substr($link,0,(strpos(substr($link,8),"/")+8))// CODIGO AVANZADO (NO TOCAR :-)  )?></a><br>
        <? 
        
}

}
?>
NOTA: Usa el codigo del cometario anterior para el include
  #7 (permalink)  
Antiguo 12/01/2015, 14:49
 
Fecha de Ingreso: diciembre-2013
Mensajes: 37
Antigüedad: 10 años, 4 meses
Puntos: 1
Respuesta: estructura de una web para parsear

Cita:
Iniciado por enlinea777 Ver Mensaje
Hola Andreiya

aqui te dejo el codigo para parsear en google, espero te ayude.

aqui el ejemplo en vivo

[URL="http://creaelicita.cl/ayuda/foro_1117870_v1.php?texto=gatos"]http://creaelicita.cl/ayuda/foro_1117870_v1.php?texto=gatos[/URL]

usa la variable texto para buscar lo que quieras

Código PHP:
<?php
include('simple_html_dom.php'); 
  
        
$handler curl_init();// iniciar el comando CURL POR PHP
        
curl_setopt($handlerCURLOPT_URL'https://www.google.com/search?q=gatitos&ie=utf-8&oe=utf-8');
        
curl_setopt($handlerCURLOPT_RETURNTRANSFER1);
        
curl_setopt($handler,CURLOPT_USERAGENT,'Mozilla/4.0 (PDA; PalmOS/sony/model prmr/Revision:1.1.54 (en)) NetFront/3.0');// IMPORTANTE HACRCE PASAR POR UNA PALM TE ASEGURA UN RESULTADO MAS LIMPIO
        // OTROS COMPLEMENTOS
        
curl_setopt($handler,CURLOPT_HOTS,'www.google.com');
        
curl_setopt($handler,CURLOPT_ACCEPT,'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
        
curl_setopt($handler,CURLOPT_ACCEPTLANGUAGE,'es-cl,es;q=0.8,en-us;q=0.5,en;q=0.3');
        
curl_setopt($handler,CURLOPT_COOKIE,'NID=67=nHypWZWZS_T-SlSSuXAJTMhZB4gIvYOtOUP0vjoOPGjE8HYSK0ONSY1luZeWkIvH7iqpVdxH6Xf0BEcTqIj1Y6rrZJQ8z8O3HdX2gFekYHs-Eh0dJ6d_aw8Lq2VHtFwlBLVxEk5P4t77cyvTNK7EeEDrmUtrgoBFEX72EDgPHvJ42GJshGlaYLTa-Gr2pguDypLsCNRySdDN5dyxSnIn; SID=DQAAAAICAABeps3N4B3sU4HJ1vIE97a98emN7n94wpIoW5MJRRMNHexTSzoY_NfXTV8CIZ2NW6vnC0K2G8Sq7YqEWI_pEEuHPvWA4CkLxMFMC_An3BkmgX9ZD9MywaLuUGlMF-3Hyf8PgJbc6NYTqaN1Qo6duT3aMplkRVmVMHVdPfq46VffGIq0ayEzcd-uYmTjE7ckT0rpb-RqrcuGUyvqUZPnvFbCwDqcGtSd98bEj6bfwDyG3Ap6apF_v6DmQan-eNkIE1FEXzHB0bXd_nKROCm4FKJrz1tONYJGRVP43XHbgv89UkEyF9OAgiZvUB3PyFUU4ihkv0raYQZBcLiiY6NvRPANpKfN885MNwaFPHLHR1woTELjrorj-YASvVxr8ai3blqs7nFI8fy1S_DwWe_weJxn6PvZsX757xh0GQBMOfKznHlx6XFNKaVmwlhdhwlVY3UfJO-svRhpOSCReeLzFlhye19A1sSY-HCTlBEZVI6eXKhCkb5YzTr5DZM79bC2Px45RBu39M4i8znhjIgNe3a6xC5P6crGjAkes6qx7LEdc9keJB27X0ZN5e_S6wccrp9zbMGn9jJ03hW47A4FISDqhyETipj40sXIx0xEDungKl9U_4cGbAMeyWdjwc-HqVu4jSHNw5mOwNloTY_zFWlAdXnalvNzRoBEiRWHHh59Z9rtvxnekr33q7t3xMqo8Gg; HSID=AbEEV6EbfNcqLumK2; SSID=ApgzPR1FFU_d2zQU7; APISID=HugUvpV0VR3gJMIQ/AVyBODG-BvQpt5r7O; SAPISID=oCYqAoqsB6_lPdrh/AFw8b_LEX6RubiuAr; PREF=ID=b904718712cc0d24:U=39272d8d77cdf325:FF=0:LD=es-419:NR=100:TM=1407341101:LM=1421081382:GM=1:SG=1:S=Iy_OMhncF0Buyype; GOOGAPPUID=739; enabledapps.uploader=0; llbcs=0');
        
curl_setopt($ch,CURLOPT_CONNECTION,'keep-alive');
        
curl_setopt($ch,CURLOPT_PRAGMA,'no-cache');
        
curl_setopt($ch,CURLOPT_CACHECONTROL,'no-cache');
    
$response curl_exec($handler);// EJECURAT Y DEVOLVER EL RESULTADO
    
    
curl_close($handler); //CERRAR LA LLAMADA
    
    // ALGUNOS FILTROS  PARA EL HTML  
    
$response ereg_replace("<script([^>]*)>([^<]*)<\/script>",'',$response);
    
$response preg_replace('/(<[^>]+) style=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) class=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) bgcolor=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) border=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) title=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) onDblClick=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) onClick=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) class=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) role=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) tabindex=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) aria-expanded=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) aria-haspopup=".*?"/i''$1'$response);
    
$response preg_replace('/(<[^>]+) data-ved=".*?"/i''$1'$response);
    
$response preg_replace('/\<ol\>/i''$1'$response);$response preg_replace('/\<\/ol\>/i''$1'$response);
    
$response preg_replace('/\<h.\>/i''$1'$response);$response preg_replace('/\<\/h.\>/i''$1'$response);
    
$response preg_replace('/\<br\>/i''$1'$response);
    
$response preg_replace('/\<span\>/i''$1'$response);$response preg_replace('/\<\/span\>/i''$1'$response);
    
$response preg_replace('/\<ul\>/i''$1'$response);    $response preg_replace('/\<\/ul\>/i''$1'$response);
    
$response preg_replace('/\<b\>/i''$1'$response);    $response preg_replace('/\<\/b\>/i''$1'$response);
    
$response ereg_replace("<cite([^>]*)>([^<]*)<\/cite>",'',$response);
    
$response =preg_replace('/thead/''tr'$response);
    
$response =preg_replace('/\<\/th>/''</td> '$response);
    
$response preg_replace('/tbody/''tr'$response);

?><h1>Links</h1><?      
$html 
str_get_html($response);  // USAR  simple_html_dom   PARA FACILITAR LA BUSQUEDA
$ret $html->find('a'); // BUSCAR LOS LINKS

    
foreach($ret as $element){// RECORRER LOS LINKS
        
$link=$element->href;
        
$link='http'.preg_replace('/http(.*)http/'''$link);// REEMPLASA LOS LINKS DE GOOGLE
        
$texto=$element->plaintext;// TEXTO DEL LINK
        
$q=substr($link,0,5);// LOS PRIMEROS 5 CARACTERES PARA FILTRAR RESULTADOS ENTRE PAGINAS DE GOOGLE Y BUSQIEDAS
        
        
if($q=='http:' && $texto!='Cached'){// FILTRADO ENTRE PAGINAS DE GOOGLE Y BUSQIEDAS
        
?>
        <a href="<?=$link?>"><?=$texto?$texto:substr($link,0,(strpos(substr($link,8),"/")+8))// CODIGO AVANZADO (NO TOCAR :-)  )?></a><br>
        <? 
        
}

}
?>
NOTA: Usa el codigo del cometario anterior para el include
Gracias cielo, llevo días trasteando con las opciones de esa página, pero no me salía muy bien. Al final construí una función personalizada.
Me guardo este código para probarlo en otras cosillas ;)
Añado un tema importante que, por ser novata, no sabía.

La función file_get_contents,( normalmente me ha funcionado más lenta que cURL, pero alguna vez también sucede con cURl)me ha tenido loca unos días.

El motivo, que no almacena fielmente lo que muestra la web desde tu ordenador.
Ejemplo:
Descargaba una página con file_get_contents, y al introducir las búsquedas en la cadena almacenada no me salían los resultados.

Después de mirar y remirar el código me di cuenta de que era el user-agent lo que fallaba. Al mandar la petición con cURL(que permite configurar el user-agent) ya la página sí me devolvía la versión que me hacía falta(en la que aparecían los links)

Todo esto contado parece una tontería, pero me ha dado dolores de cabeza y reescribir el código mil veces hasta que me di cuenta de que la cadena que devolvía uno y otro eran diferentes.

Doy el tema por solucionado y te mando un besito grande enlinea777 por la ayudita prestada

Etiquetas: estructura, html, parsear
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:18.