Inicie el acceso directo a la URL del sitio web usando Python

En este artículo, vamos a iniciar sitios web favoritos usando accesos directos, para esto, usaremos los módulos sqlite3 y webbrowser de Python para iniciar sus sitios web favoritos usando accesos directos.

Tanto sqlite3 como webbrowser son parte de la biblioteca estándar de python, por lo que no necesitamos instalar nada por separado. 

La mejor parte de esto es que debido a que estamos usando una base de datos para almacenar los sitios web y sus accesos directos, sus sitios web favoritos permanecerán guardados en la base de datos incluso si cerramos la terminal o apagamos la PC. Por lo tanto, no necesita agregar sus sitios web favoritos a la base de datos cada vez que ejecuta el script.

Acercarse:

  • Creamos una nueva base de datos si aún no existe y la conectamos a nuestra base de datos.
  • Creamos diferentes funciones para obtener datos de la base de datos, abrir un sitio web, agregar un nuevo sitio web a la base de datos y eliminar un sitio web de la base de datos.
  • Llamamos a un bucle while para que nuestro programa pueda escuchar los comandos del usuario hasta que se cierre
  • Pedimos una respuesta del usuario para realizar una tarea específica

A continuación se muestra la implementación:

Python3

import webbrowser  
import sqlite3        
  
# connecting to sqlite and creating
# an actual database
conn = sqlite3.connect("favorites.db")
  
c = conn.cursor()
  
c.execute("""CREATE TABLE IF NOT EXISTS favorites
        (title TEXT, url TEXT)""")
  
# Created a table named favorites
# (if it didn't already existed).
# Then inserted the headers  title 
# (which takes text as input)
# and  url  (which takes text as input)
def get_data():
    """
    Used to extract data from our database
    """
      
    c.execute('''SELECT * FROM favorites''')
    results = c.fetchall()
    return results
  
  
def get_fav(titl):
    """
    Used to extract the favorite website
    """
      
    c.execute('''SELECT * FROM favorites WHERE title=?''',
              (titl, ))
    return c.fetchone()
  
def add_fav(titl, url):
    """
    Used to add a new favorite website
    """
      
    c.execute("""INSERT INTO favorites (title, url) VALUES (?, ?)""",
              (titl, url))
    conn.commit()
  
def remove_fav(titl):
    """
    Used to remove a favorite website from the database
    """
      
    c.execute('''DELETE FROM favorites WHERE title=?''',
              (titl, ))
    conn.commit()
  
# A loop to listen to commands from the user
while True:
    print()
  
    # printing each statement like 
    # this to keep the code clean
    print("Press v to visit a favorite,", end=" ")
    print("ls for list,", end=" ")
    print("add to add a new item,", end=" ")
    print("rm to delete,", end=" ")
    print("q to quit:", end=" ")
  
    # taking input command from the user
    response = input("")
  
    if response.lower() == "v":
        shortcut = input("Enter the shortcut for the website: ")
        record = get_fav(shortcut)
  
        try:
            # opening the selected website in the browser
            webbrowser.open(record[1])
  
        except TypeError:
            # if we don't have the shortcut 
            # in the database, print this:
            print('This shortcut does not exist in the database')
  
    elif response.lower() == "ls":
        # printing the items in the database
        print(get_data())
  
    elif response.lower() == "add":
        # adding a new website to the database
        destination = input(
            "Enter URL for the shortcut (Example -> https://xyz.com): ")
  
        # adding the shortcut to the above website in
        # the database
        shortcut = input("Enter the shortcut for the URL: ")
  
        add_fav(shortcut, destination)
  
    elif response.lower() == "rm":
        
        # removing an item from the database
        shortcut = input(
            "Enter the shortcut for the URL you want to remove: ")
        remove_fav(shortcut)
        print("Removed Successfully")
  
    elif response.lower() == "q":
        break
  
    else:
        print("Enter a valid command")

Producción:

Ejecución de código

Publicación traducida automáticamente

Artículo escrito por urvishmahajan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *