Skip to content

Instantly share code, notes, and snippets.

@harpiechoise
Last active September 13, 2017 08:32
Show Gist options
  • Save harpiechoise/482c8d653638d7c4fc8f2bfeb5ec52bb to your computer and use it in GitHub Desktop.
Save harpiechoise/482c8d653638d7c4fc8f2bfeb5ec52bb to your computer and use it in GitHub Desktop.
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import csv
myurl = "https://www.lider.cl/supermercado/category/Despensa/Pastas/Fideos-Largos/_/N-673e4a"
uClient = uReq(myurl)
page_html = uClient.read()
nombres = []
marcas = []
gramaje = []
precios = []
page_soup = soup(page_html,"html.parser")
container = page_soup.find_all("a",{"data-level":"3"})
links = []
link = "https://www.lider.cl"
for cont in container:
for i in cont:
links.append(link+cont["href"])
for link1 in links:
my_url = link1 + "?N=&No=0&Nrpp=120&isNavRequest=Yes"
print("Conectando a: "+my_url)
try:
uClient = uReq(my_url)
except:
print("Error Continuando...")
pass
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html,"html.parser")
container = page_soup.find_all("div",{'class':'box-product'})
for containers in container:
raw_marca = containers.find_all("span",{"class":'product-name'})
raw_nombre = containers.find_all("span",{'class':'product-description'})
raw_gramos = containers.find_all("span",{'class':'product-attribute'})
raw_precio = containers.find_all("span",{'class':'price-sell'})
marca = raw_marca[0].text
nombre = raw_nombre[0].text
gramos = raw_gramos[0].text
precioes = raw_precio[0].text
precio = " ".join(precioes.split())
nombres.append(nombre)
precios.append(precio)
gramaje.append(gramos)
marcas.append(marca)
f = open("Lider dia 001.csv","w", encoding="utf-8")
f.write("Producto,Precio"+"\n")
for i in range(len(precios)):
f.write((nombres[i].replace(",","|")+" "+marcas[i].replace(",","|")+" "+gramaje[i].replace(",","|")+","+precios[i].replace(",","|")+"\n"))
f.close()
nombre = []
precio = []
file = input("Escriba el nombre del archivo (Sin Extension): ")
filef = file + ".csv"
with open(filef, encoding="utf8") as f:
y = csv.reader(f,delimiter =",")
for i in y:
nombre.append(i[0])
precio.append(i[1])
nombre2 = []
precio2 = []
file = input("Escriba el nombre del archivo a comparar (Sin Extension): ")
filef = file + ".csv"
with open(filef, encoding="utf8") as f:
y = csv.reader(f,delimiter =",")
for i in y:
nombre2.append(i[0])
precio2.append(i[1])
for j in range(len(precio2)):
if not (precio[j] == precio2[j]):
print("Nombre: " + nombre2[j] + "\n" + "Precio antiguo: " + precio[j] + "\n Precio Nuevo: " + precio2[j] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment