Python Forum
[Tkinter] Problems to display Web Scraping values in Tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Problems to display Web Scraping values in Tkinter (/thread-26608.html)



Problems to display Web Scraping values in Tkinter - Lucas_Ribeiro - May-07-2020

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.

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():
    raw_data = urllib.request.urlopen("https://i.imgur.com/AHLqtt0.jpg").read()
    im = Image.open(io.BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label1 = Label(root, image=image).pack()

def img_procyon():
    raw_data = urllib.request.urlopen("https://i.imgur.com/TQyCnfD.jpg").read()
    im = Image.open(io.BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label1 = Label(root, image=image).pack()

root = Tk()

with urllib.request.urlopen("http://www.cabaleasy.com") as url: page = url.read()

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...

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
If I just try the texts it's working:

import urllib.request
from bs4 import BeautifulSoup

with urllib.request.urlopen("http://www.cabaleasy.com") as url: page = url.read()

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:])