Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- import tkinter.messagebox
- import urllib.request
- from bs4 import BeautifulSoup
- import os
- import ssl
- if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
- getattr(ssl, '_create_unverified_context', None)):
- ssl._create_default_https_context = ssl._create_unverified_context
- def store():
- conn = sqlite3.connect('vinilos.db')
- conn.execute("DROP TABLE IF EXISTS VINILO")
- conn.execute('''CREATE TABLE VINILO
- (ID INTEGER PRIMARY KEY AUTOINCREMENT,
- INTERPRETE TEXT NOT NULL,
- TITULO TEXT NOT NULL,
- GENERO TEXT NOT NULL,
- PAIS TEXT NOT NULL,
- COMPAÑIA TEXT NOT NULL,
- PRECIO FLOAT NOT NULL);''')
- conn.commit()
- cursor = conn.execute("SELECT COUNT(*) FROM VINILO")
- tkinter.messagebox.showinfo(
- "Base Datos", f"BD creada correctamente, hay {str(cursor.fetchone()[0])} registros")
- conn.close()
- def cargar(url):
- conn = sqlite3.connect('vinilos.db')
- for i in range(1, 3):
- url = f'https://www.disc-order.com/es/results.php?ARTISTATYPE=begins&FAMCODIGO=LP&STOCKTYPE=instock&TYPE=adv&orderfiel=&page={i}'
- f = urllib.request.urlopen(url)
- soup = BeautifulSoup(f, 'lxml')
- items = soup.find(id="newresults").find_all("div", class_="item")
- discos = []
- for item in items:
- enlace = item.find("a", class_="l")["href"]
- enlace = f'https://www.disc-order.com/{enlace}'
- g = urllib.request.urlopen(enlace)
- detalles = BeautifulSoup(g, 'lxml')
- divDetails = detalles.find(id="newdetails").find(
- "div", class_="details")
- interprete = divDetails.find_all(
- "span", class_="dt", string="Intérprete / Grupo: ")
- interprete = interprete[0].find_next_sibling(
- "span").contents[0] if len(interprete) else "-"
- titulo = divDetails.find_all(
- "span", class_="dt", string="Titulo / Descripción: ")
- titulo = titulo[
- 0].find_next_sibling("span").contents[0] if len(titulo) else "-"
- genero = divDetails.find_all(
- "span", class_="dt", string="Género: ")
- genero = genero[
- 0].find_next_sibling("span").contents[0] if len(genero) else "-"
- pais = divDetails.find_all("span", class_="dt", string="País: ")
- pais = pais[
- 0].find_next_sibling("span").contents[0] if len(pais) else '-'
- compañia = divDetails.find_all(
- "span", class_="dt", string="Compañía: ")
- compañia = compañia[
- 0].find_next_sibling("span").contents[0] if len(compañia) else '-'
- precioCompleto = detalles.find(id="newdetails").find_all(
- "span", class_="ii")[0].contents[0]
- precioCompleto = precioCompleto.split(" ")
- precio = precioCompleto[1].replace(
- ",", ".") if len(precioCompleto) > 1 else 0
- print([interprete, titulo, genero, pais, compañia, precio])
- discos.append([interprete, titulo, genero, pais, compañia, precio])
- cur = conn.cursor()
- cur.executemany(
- "INSERT INTO VINILO (INTERPRETE, TITULO, GENERO, PAIS, COMPAÑIA, PRECIO) VALUES (?,?,?,?,?,?)", discos)
- conn.commit()
- cur.close()
- conn.close()
- cargar("https://www.discorder.com/es/results.php?ARTISTATYPE=begins&FAMCODIGO=LP&STOCKTYPE=instock&TYPE=adv&orderfield=6")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement