I´m developing a simple reactive programming script example to download images from a web, but when I execute the script in VScode I do not get any type of output, I already tried creating a VirtualEnv
In the terminal i get this output:
PS C:\Users\ernes\Desktop\paradigmas> & C:/Users/ernes/AppData/Local/Programs/Python/Python38/python.exe c:/Users/ernes/Desktop/paradigmas/jose.py PS C:\Users\ernes\Desktop\paradigmas>
CODE:
import base64
import aiohttp
import asyncio
import aiofiles
import io
import rx
from tkinter import *
from tkinter.ttk import *
from urllib.request import urlopen
from PIL import ImageTk, Image
from bs4 import BeautifulSoup
class App:
async def getSourceCode(url):
async with aiohttp.ClientSession() as clientSession:
serverResponse = await clientSession.get(url)
sourceCode = await serverResponse.text()
return sourceCode
async def main(self, urlToProcess='https://es.wikipedia.org/wiki/38M_Toldi'):
print(urlToProcess)
sourceCode = self.getSourceCode(urlToProcess)
parsedSource = BeautifulSoup(sourceCode, 'html.parser')
for imgItem in parsedSource.find_all('img', src=True):
if imgItem['src'].find('http') == True:
imgSrc = imgItem['src']
if len(imgItem['alt']) > 0 :
imgName = imgItem['alt']
else:
imgName = imgSrc
if not imgName in self.imgs:
async with aiohttp.ClientSession() as clientSession:
try:
serverResponse = await clientSession.get(imgSrc)
imgBytes = await serverResponse.read(serverResponse)
if imgBytes:
self.bytesfoto.append(imgBytes)
print(f'{imgName} : {imgSrc}')
except:
print('Error al descargar la foto')
"""
Definicion de los atributos de la clase iniciales donde se guardan los datos que se usaran en el tkinter para la GUI
contador -- es el que lleva la cuenta de la cantidad de imágenes que se añaden a la lista
window -- define la ventana de la GUI
imgs -- lista en la que se guardan las URLs de las imágenes
bytesfoto -- lista en la que se guardan las fotos en bytes para poder guardarlas en memoria
objImg -- variable que convierte los links de las fotos en observables
"""
def __init__(self):
self.contador = 0
self.window = Tk()
self.window.title = "Reactive-Programming"
self.imgs = []
self.bytesfoto = []
self.objImg = rx.from_iterable(self.imgs)
CodePudding user response:
Add this to the bottom, outside of the class
theobj = asyncio.run(App().main())
theobj()
The script should work after, with some errors.