May-08-2020, 02:35 AM
I'm making a web scrapping on a website for a private game server using Python. In a part of this website prints the result of the last nation war winner and show 2 messages: "Capella Ganhou!!" and "Procyon Ganhou!!" using the BeautfulSoup library. I made a condition for each event for show the winner nation image into the Label by an url. But for some reason it shows always the last image url and I don't know what is happening. Here is the code below:
from tkinter import * import urllib.request from bs4 import BeautifulSoup from IPython.display import Image, display from PIL import Image, ImageTk from io import BytesIO def capella_tg(): resultado_tg = StringVar() try: resultado_tg.set(soup.find_all("font")[5].string) label_resultado_tg = Label(root, textvariable=resultado_tg, font=("Consolas", 25)).pack() except: return False def procyon_tg(): resultado_tg = StringVar() try: resultado_tg.set(soup.find_all("font")[4].string[3:]) label_resultado_tg = Label(root, textvariable=resultado_tg, font=("Consolas", 25)).pack() except: return False root = Tk() root.title("Resultado TG Cabal Ghost") with urllib.request.urlopen("http://www.cabaleasy.com") as url: page = url.read() soup = BeautifulSoup(page, "html.parser") if capella_tg(): url = "https://i.imgur.com/AHLqtt0.jpg" with urllib.request.urlopen(url) as u: raw_data = u.read() im = Image.open(BytesIO(raw_data)) image = ImageTk.PhotoImage(im) label = Label(image=image).pack() else: procyon_tg() url = "https://i.imgur.com/TQyCnfD.jpg" with urllib.request.urlopen(url) as u: raw_data = u.read() im = Image.open(BytesIO(raw_data)) image = ImageTk.PhotoImage(im) label = Label(image=image).pack() root.mainloop()