Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Scrapping & WordPress

Web Scrapping & WordPress

Cómo realizar web scrapping en PHP y usando PHP Simple HTML DOM Parser con WordPress para obtener y almacenar la información. Exposición para la comunidad WordPress Perú.

More Decks by César J. Aquino Maximiliano

Other Decks in Technology

Transcript

  1. Web Scraping con
    WordPress
    César Aquino Maximiliano - @cesjam7

    View Slide

  2. ¿Qué es Web Scraping?
    Web Scraping es una técnica utilizada
    mediante software para extraer
    información de sitios web.

    View Slide

  3. Técnicas para hacer Web Scraping

    Copy & Paste

    Expresiones regulares

    Protocolo HTTP

    Parses HTML

    Aplicaciones para Web Scraping

    View Slide

  4. Técnicas para hacer Web Scraping

    Copy & Paste

    Expresiones regulares

    Protocolo HTTP

    Parses HTML

    Aplicaciones para Web Scraping

    View Slide

  5. View Slide

  6. View Slide

  7. Solicita
    información

    View Slide

  8. Solicita
    información
    Obtiene
    información

    View Slide

  9. Solicita
    información
    Obtiene
    información
    Filtra la
    información

    View Slide

  10. Solicita
    información
    Obtiene
    información
    Filtra la
    información
    Procesa la
    información

    View Slide

  11. Solicita
    información
    Obtiene
    información
    Filtra la
    información
    Procesa la
    información
    Guarda la
    información

    View Slide

  12. Solicita
    información
    Obtiene
    información
    Filtra la
    información
    Procesa la
    información
    Guarda la
    información

    View Slide

  13. PHP Simple HTML DOM Parser

    A HTML DOM parser written in PHP5+ let you manipulate
    HTML in a very easy way!

    Require PHP 5+.

    Find tags on an HTML page with selectors just like jQuery.

    Extract contents from HTML in a single line.
    http://simplehtmldom.sourceforge.net/

    View Slide

  14. Ejemplo simple de
    cómo extraer usando
    PHP Simple HTML DOM
    Parser
    // Create DOM from URL or file
    $html =
    file_get_html('http://jovenred.com/');
    // Find all images
    foreach($html->find('img') as $element)
    {
    echo $element->src.'
    ';
    }
    // Find all links
    foreach($html->find('a') as $element)
    {
    echo $element->href.'
    ';
    }
    ?>

    View Slide

  15. Tres formas para
    obtener un DOM Object
    // Create a DOM object from a string
    $html =
    str_get_html('Hello!<
    /html>');
    // Create a DOM object from a URL
    $html =
    file_get_html('http://jovenred.com/');
    // Create a DOM object from a HTML file
    $html = file_get_html('test.htm');
    ?>

    View Slide

  16. Cómo buscar un
    elemento HTML
    // Find all anchors, returns a array of
    element objects
    $ret = $html->find('a');
    // Find (N)th anchor, returns element
    object or null if not found (zero based)
    $ret = $html->find('a', 0);
    // Find all with the id attribute
    $ret = $html->find('div[id]');
    // Find all which attribute id=foo
    $ret = $html->find('div[id=foo]');
    ?>

    View Slide

  17. ¿Y donde entra
    WordPress?

    View Slide

  18. Solicita
    información
    Obtiene
    información
    Filtra la
    información
    Procesa la
    información
    Guarda la
    información

    View Slide

  19. Cómo ingresar la
    información a nuestra
    base de datos con
    WordPress
    // Create DOM from URL or file
    $html =
    file_get_html('http://jovenred.com/');
    // Find all images
    foreach($html->find('.post') as $element)
    {
    $title = $element->find('h2
    a')->plaintext;
    $content = $element->find('p',
    0)->plaintext;
    wp_insert_post( array(
    'post_title' => $title;
    'post_content' => $content;
    ) );
    }
    ?>

    View Slide

  20. Cómo ingresar la
    información a nuestra
    base de datos con
    WordPress
    // Create DOM from URL or file
    $html =
    file_get_html('http://jovenred.com/');
    // Find all images
    foreach($html->find('.post') as $element)
    {
    $title = $element->find('h2
    a')->plaintext;
    $content = $element->find('p',
    0)->plaintext;
    wp_insert_post( array(
    'post_title' => $title;
    'post_content' => $content;
    ) );
    }
    ?>

    View Slide

  21. https://codex.wordpress.org/Function_Reference/wp_insert_post

    View Slide

  22. #DataBomberos
    Data Bomberos Perú es una página no oficial que muestra
    información recolectada de la central de emergencias del Cuerpo
    General de Bomberos Voluntarios del Perú.
    La información es obtenida por la página Emergencias Diarias
    que comparte el Cuerpo de Bomberos de forma pública en el
    siguiente link http://www.bomberosperu.gob.pe/po_diario.asp.
    http://databomberos.com

    View Slide

  23. View Slide

  24. #DataBomberos
    https://github.com/cesjam7/cbp-emergencies

    View Slide

  25. #DataBomberos
    Emergencias del 14/05/2015
    Emergencias por categoría Mapa de calor

    View Slide

  26. Hora de codear
    Vamos a scrapear los resultados de búsqueda de Foursquare:

    View Slide

  27. Hora de codear
    Vamos a scrapear los resultados de búsqueda de Foursquare:
    $html = file_get_html($_POST['link']);
    $c = 0;
    foreach($html->find('.contentHolder') as $result) {
    $c++;
    echo '';
    echo ''.$c.'';
    echo ''.$result->find('.categoryName', 0)->plaintext.'';
    echo ''.$result->find('.venueName a', 0)->plaintext.'';
    echo '';
    }

    View Slide

  28. Muchas gracias

    Twitter: @cesjam7

    Web Personal: cesar.pe

    Empresa: orange612.com

    Presentaciones: speakerdeck.com/cesjam7

    Repositorios: github.com/cesjam7

    View Slide

  29. Muchas gracias

    Twitter: @cesjam7

    Blog: jovenred.com

    Empresa: orange612.com

    Presentaciones: speakerdeck.com/cesjam7

    Repositorios: github.com/cesjam7
    Recuerden: En caso de emergencia llamar al 1-1-6

    View Slide