Giter VIP home page Giter VIP logo

scraper-de-noticias's Introduction

scraper-de-noticias

Este repositorio contiene un script de Python que realiza web scraping en el sitio web de La República Colombia para extraer noticias y guardarlas en archivos de texto plano.

Configuración del entorno de trabajo y buenas prácticas

  • Crear una carpeta para el proyecto.

  • Iniciar un repositorio de Git.

  • Crear un entorno virtual.

  • Crear el archivo .gitignore

  • Instalar las librerías, en este caso:

     pip install -r requirements.txt
    1. Request: Para realizar peticiones.
    2. Lxml: Para manipular el documento html y utilizar Xpath
  • Actualizar pip si este lo sugiere, también es una buena práctica:

	python.exe -m pip install --upgrade pip

Construcción de las expresiones de XPath

Extraer los links de las noticias:

'//h2/a[contains(@class, "Sect")]/@href'

Nota: Por alguna razón que la librería requests cambia los nodos h2 por text-fill. Solamente pasa a la hora de obtener los links. Por eso en el código se coloca text-fill en lugar de h2.

En el navegador:

$x('//h2/a[contains(@class, "Sect")]/@href').map(x => x.value)

Extraer el título de la noticia:

'//h2[not(@class)]/span/text()'

En el navegador:

$x('//h2[not(@class)]/span/text()').map(x => x.wholeText)

Extraer el resumen de la noticia:

'//div[@class="lead"]/p/text()'

En el navegador:

$x('//div[@class="lead"]/p/text()').map(x => x.wholeText)

Extraer el cuerpo de la noticia

Con este Xpath se obtienen los nodos 'p' dentro del body.

'//div[@class="html-content"]/p'

Funciones

parse_notice(link, today)

Función que realiza el scraping de un artículo de noticias y guarda la información en un archivo de texto.

Args:

  • link (str): Link al artículo de noticias.
  • today (str): Fecha actual en formato dd-mm-yy.

parse_home()

Función que realiza el scraping de la página principal de La República y llama a la función parse_notice para cada artículo de noticias encontrado.

scraper-de-noticias's People

Contributors

bssanchezr7 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.