Skip to content

Instantly share code, notes, and snippets.

@fransafu
Created February 25, 2018 17:52
Show Gist options
  • Save fransafu/3e5865ae1da7a88e38f40ad72c264b27 to your computer and use it in GitHub Desktop.
Save fransafu/3e5865ae1da7a88e38f40ad72c264b27 to your computer and use it in GitHub Desktop.
Web scraping of site web informeperros.com
# -*- coding: utf-8 -*-
from pprint import pprint
from bs4 import BeautifulSoup
import requests
import json
URL = 'http://informeperros.com/lista-de-todas-las-razas-de-perros-por-orden-alfabetico/'
def main():
# Realizamos un GET a la pagina web
req = requests.get(URL)
# Utilizamos un parse con el formato lxml
soup = BeautifulSoup(req.text, "lxml")
# Buscamos todos los <td></td> que existan en el sitio web
columnas = soup.find_all('td')
# Crear diciconario de almacenamiento
diccionarioRazas = []
# Objeto auxiliar
dicAux = {}
for columna in columnas:
# Buscamos el tag <img> dentro de los datos ya filtrados por <td>
imagen = columna.find('img')
if (imagen != None):
urlImagen = imagen["src"]
nombreImagen = urlImagen.split("/")[len(urlImagen.split("/")) - 1]
# Descargar imagenes
with open("imagenes/" + nombreImagen, "wb") as archivo:
img = requests.get(urlImagen, stream=True)
archivo.write(img.content)
dicAux["urlImagen"] = imagen["src"]
dicAux["raza"] = imagen["title"]
diccionarioRazas.append(dicAux)
dicAux = {}
# Grabar archivo con las razas
with open("razas.json", "w") as archivoRazas:
jsonText = json.dumps(diccionarioRazas, indent=4, sort_keys=True)
archivoRazas.write(jsonText)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment