Python Forum
[Tkinter] Problems to display Web Scraping values in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Problems to display Web Scraping values in Tkinter
#1
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:])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems trying to position images with Tkinter emont 3 674 Dec-12-2023, 07:20 AM
Last Post: menator01
  [TKINTER] Problems creating directories in a selected path Vulera 2 2,731 Aug-10-2021, 06:38 PM
Last Post: Vulera
  Tkinter having problems with packing labels? wallgraffiti 0 1,508 Aug-02-2020, 09:26 AM
Last Post: wallgraffiti
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,651 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  How to display results from terminal window onto tkinter. buttercup 0 3,603 Jul-21-2020, 04:41 AM
Last Post: buttercup
  [Tkinter] Tkinter adding entry values scratchmyhead 1 2,165 May-04-2020, 05:21 AM
Last Post: Yoriz
  tkInter and Pillow don't display any images in GUIs - program gives errors instead SomeRandomGuy 9 10,608 Oct-29-2019, 02:57 PM
Last Post: SomeRandomGuy
  Python tKinter sQL. How to add a column of values? dewijones67 4 4,696 Feb-14-2019, 07:33 AM
Last Post: dewijones67
  Display and update the label text which display the serial value jenkins43 5 8,993 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 3,836 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020