May-07-2020, 12:36 AM
I am trying to display an image inside a Tkinter window through Web Scraping displaying a message from a certain point on the website that says in Portuguese "Capella Ganhou" or "Procyon Ganhou". I tried to look in different forums for a solution but I couldn't find any that fixes the error in my situation. I also tested with print to make sure the string exists and is returning its value and also encapsulated it into a variable. The source code and the error are below.
If I just try the texts it's working:
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 |
from tkinter import * import urllib.request from bs4 import BeautifulSoup from IPython.display import Image, display from PIL import Image, ImageTk import io def capella_tg(): resultado_tg = StringVar() resultado_tg. set (soup.find_all( "font" )[ 5 ].string) label_resultado_tg = Label(root, textvariable = resultado_tg).pack() def procyon_tg(): resultado_tg = StringVar() resultado_tg. set (soup.find_all( "font" )[ 4 ].string[ 3 :]) label_resultado_tg = Label(root, textvariable = resultado_tg).pack() def img_capella(): im = Image. open (io.BytesIO(raw_data)) image = ImageTk.PhotoImage(im) label1 = Label(root, image = image).pack() def img_procyon(): im = Image. open (io.BytesIO(raw_data)) image = ImageTk.PhotoImage(im) label1 = Label(root, image = image).pack() root = Tk() soup = BeautifulSoup(page, "html.parser" ) #print(soup.find_all("font")[5].string) try : capella_tg() except : procyon_tg() if capella_tg(): img_capella() elif procyon_tg(): img_procyon() root.mainloop() |
Error:Traceback (most recent call last):
File "C:/Users/LucasDEV/PycharmProjects/LucasDEV/WEB_SCRAPPING/TESTES.py", line 49, in <module>
elif procyon_tg():
File "C:/Users/LucasDEV/PycharmProjects/LucasDEV/WEB_SCRAPPING/TESTES.py", line 17, in procyon_tg
resultado_tg.set(soup.find_all("font")[4].string[3:])
TypeError: 'NoneType' object is not subscriptable
I also tried using the try/except into the function, but it's getting the same error...1 2 3 4 5 6 7 |
def capella_tg(): resultado_tg = StringVar() try : resultado_tg. set (soup.find_all( "font" )[ 5 ].string) label_resultado_tg = Label(root, textvariable = resultado_tg).pack() except : pass |
1 2 3 4 5 6 7 8 9 10 11 12 |
import urllib.request from bs4 import BeautifulSoup soup = BeautifulSoup(page, "html.parser" ) try : print (soup.find_all( "font" )[ 5 ].string) #At this moment, it will not be showing, cuz the status changes every hour except : print (soup.find_all( "font" )[ 4 ].string[ 3 :]) |