Código de Python - Visor de código HTML

Imágen de perfil
Val: 712
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Visor de código HTMLgráfica de visualizaciones


Python

Actualizado el 18 de Diciembre del 2021 por Antonio (75 códigos) (Publicado el 19 de Octubre del 2021)
1.999 visualizaciones desde el 19 de Octubre del 2021
GUI que muestra el código HTML de una página, ingresando su URL. También permite guardar dicho código en formato texto mediante la función "SAVE".
ght

Requerimientos

Se necesita tener instalada la librería 'pyperclip'.

1.0

Actualizado el 18 de Diciembre del 2021 (Publicado el 19 de Octubre del 2021)gráfica de visualizaciones de la versión: 1.0
2.000 visualizaciones desde el 19 de Octubre del 2021
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
from tkinter import messagebox, filedialog
import tkinter.scrolledtext as sct
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import pyperclip
import time
import threading
import os
import requests
 
class app():
    def __init__(self):
        self.root = Tk()
        self.root.title("GET HTML")
        self.root.geometry("900x600")
        self.root.configure(bg="gray70")
        self.html_display = sct.ScrolledText(self.root,width=105,height=27,bg="black",fg="white")
        self.html_display.place(x=20,y=37)
 
        self.url = StringVar()
        self.currentDir = StringVar()
        self.currentDir.set(os.getcwd())
 
        Label(self.root,text="URL",bg="gray70").place(x=20,y=487)
        Entry(self.root,textvariable=self.currentDir,width=149).place(x=0,y=0)
        Entry(self.root,textvariable=self.url,width=50,font=('arial',14)).place(x=20,y=505)
        Button(self.root,text="GET HTML",width=89,bg="azure4",command=self.init_task).place(x=20,y=533)
        Button(self.root,text="COPY URL",width=9,command=self.init_copy).place(x=580,y=505)
        Button(self.root,text="CLEAR",width=13,height=3,command=self.clear_display).place(x=669,y=505)
        Button(self.root,text="SAVE",width=13,height=3,command=self.save_html).place(x=781,y=505)
 
 
        self.root.mainloop()
 
    def copy_paste(self):
        messagebox.showinfo("COPY URL","Copy the URL you want.")
        self.ultima_copia = pyperclip.paste().strip()
        while True:
            time.sleep(0.1)
            self.copia = pyperclip.paste().strip()
            if self.copia != self.ultima_copia:
                self.url.set(self.copia)
                self.ultima_copia = self.copia
                break
 
    def is_url(self,url):
        try:
            result = urlparse(url)
            return all([result.scheme, result.netloc])
        except ValueError:
            return False
 
    def save_html(self):
        if len(self.html_display.get('1.0',END))>1:
               document = filedialog.asksaveasfilename(initialdir="/",
                          title="SAVE AS",initialfile="html",defaultextension=".txt")
               if document != "":
                   new_file = open(document,"w",encoding="utf-8")
                   lines = "URL: {}\n\n".format(self.web)
                   for l in str(self.html_display.get("1.0",END)):
                       lines=lines+l
                   new_file.write(lines)
                   new_file.close()
                   messagebox.showinfo("SAVED","HTML saved correctly.")
 
    def clear_display(self):
        self.html_display.delete('1.0',END)
 
    def BMP(self,s):
        return "".join((i if ord(i) < 10000 else '\ufffd' for i in s))
 
    def init_copy(self):
        t2 = threading.Thread(target=self.copy_paste)
        t2.start()
 
    def init_task(self):
        t = threading.Thread(target=self.get_html)
        t.start()
 
    def get_html(self):
        if self.url.get()!="":
            try:
                self.clear_display()
                is_url = self.is_url(self.url.get())
                if is_url:
                    self.web = self.url.get()
                    result = requests.get(self.web)
                    content =   self.BMP(result.text)
                    soup = BeautifulSoup(content, 'lxml')
                    self.html_display.insert(END,soup.prettify())
                else:
                    messagebox.showwarning("Invalid URL","Enter a valid URL")
                    self.url.set("")
            except Exception as e:
                messagebox.showwarning("CAN NOT GET HTML",str(e))
        else:
            messagebox.showwarning("NO URL","No URL provided")
 
if __name__=="__main__":
    app()



Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s7168