Skip to content

Instantly share code, notes, and snippets.

@irvingprog
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irvingprog/10467877 to your computer and use it in GitHub Desktop.
Save irvingprog/10467877 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import json
import requests
class ConsultarDatos:
def __init__(self, pagina_base, numero_paginas=1):
self.pagina_busquedas = ""
self.datos = []
for a in range(numero_paginas):
self.pagina_busquedas+=requests.get(pagina_base+str(a+1)).text
def validador(funcion):
def wrappper(self):
try:
return funcion(self)
except Exception, e:
return "NA"
return wrappper
def obtener_enlaces(self):
self.enlaces = re.findall('"listing_container" href="(.*)\?',self.pagina_busquedas)
print "Numero de enlaces: ",len(self.enlaces)
def obtener_contenido_todos_los_enlaces(self):
for name in self.enlaces:
self.obtener_contenido_de_enlace(name)
def obtener_contenido_de_enlace(self, enlace):
print enlace
self.pagina = requests.get(str(enlace)).text
marca = self.obtener_marca()
combustible = self.obtener_combustible()
municipio = self.obtener_municipio()
tranmision = self.obtener_transmision()
anio = self.obtener_anio()
modelo = self.obtener_modelo()
version = self.obtener_version()
precio = self.obtener_precio()
kilometraje = self.obtener_kilometraje()
self.datos.append({
"Marca": marca,
"Combustible": combustible,
"Municipio": municipio,
"Transmision": tranmision,
"Anio": anio,
"Modelo": modelo,
"Version": version,
"Precio": precio,
"Kilometraje": kilometraje
})
@validador
def obtener_marca(self):
return re.findall('Marca: <strong>(.*)</strong>',self.pagina)[0]
@validador
def obtener_combustible(self):
return re.findall('Combustible: <strong>(.*)</strong>',self.pagina)[0]
@validador
def obtener_municipio(self):
return re.findall('Municipio: <h2 class="parameter">(.*)</h2>',self.pagina)[0]
@validador
def obtener_transmision(self):
return re.findall(u'Transmisión: <strong>(.*)</strong>',self.pagina)[0]
@validador
def obtener_anio(self):
return re.findall(u'Año: <strong>(.*)</strong>',self.pagina)[0]
@validador
def obtener_modelo(self):
return re.findall('Modelo: <strong>(.*)</strong>',self.pagina)[0]
@validador
def obtener_version(self):
return re.findall(u'Versión: <strong>(.*)</strong>',self.pagina)[0]
@validador
def obtener_precio(self):
return re.findall('\&\#36;(.*)\n',self.pagina)[0]
@validador
def obtener_kilometraje(self):
return re.findall("Kilometraje:\n\t\t<strong>\n\t\t\t(.*)\n\t\t</strong>",self.pagina)[0]
import time
inicio = time.time()
datos = ConsultarDatos("http://www.segundamano.mx/tamaulipas/autos_y_camionetas?ca=30_s&cg=2020&o=",1)
datos.obtener_enlaces()
datos.obtener_contenido_todos_los_enlaces()
with open('dataset.json','w') as out:
json.dump(datos.datos, out, sort_keys = True, indent = 4)
print "Tiempo: ",time.time()-inicio," segundos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment