import requests
from selenium import webdriver
import pandas as pd
import bs4
from urllib.request import Request,urlopen
from tkinter import *
def statno():
covidTk = Tk()
covidTk.title('Data statistics of COVID-19')
lb1 = Label(covidTk,text='Country: ')
lb1.grid(row=1,column=0,padx=10,pady=10)
lb2 = Label(covidTk,text=entcon.get())
lb2.grid(row=1,column=1,padx=10,pady=10)
driverPath = 'D:\geckodriver.exe'
browser = webdriver.Firefox(executable_path=driverPath)
url = 'https://www.worldometers.info/coronavirus/#countries'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
browser.get(url)
table = browser.find_elements_by_id("main_table_countries_today")
columns = browser.find_elements_by_xpath('//thead/tr')
rows = browser.find_elements_by_xpath('//tbody/tr')
for column in columns:
for row in rows:
df = pd.DataFrame({column:{row}})
lb3 = Label(covidTk,text='No of cases: ')
lb3.grid(row=2,column=0,padx=10,pady=10)
death_cases = str(df.values['Total Cases'][lb2.cget('text')])
lb4 = Label(covidTk,text=death_cases)
lb4.grid(row=2,column=1,padx=10,pady=10)
win = Tk()
win.title('COVID-19 tracker')
web = requests.get('https://www.worldometers.info/coronavirus')
objSoup = bs4.BeautifulSoup(web.text,'lxml')
lbtitle = Label(win,text='Covid-19 Statistics')
lbtitle.grid(row=0,columnspan=2)
lbcon = Label(win,text='Country: ')
lbcon.grid(row=1,column=0,padx=10,pady=20)
conname = StringVar()
entcon = Entry(win,textvariable=conname)
entcon.grid(row=1,column=1,padx=10,pady=20)
btncheck = Button(win,text='Check data',command=statno)
btncheck.grid(row=2,column=1,padx=10,pady=10)
lbcase = Label(win,text='Coronavirus Cases: ')
lbcase.grid(row=3,column=0)
total = objSoup.find_all('div',{'class':'maincounter-number'})
total_cases = total[0]
lbdat1 = Label(win,text=total_cases.text)
lbdat1.grid(row=3,column=1)
lbdeaths = Label(win,text='Deaths: ')
lbdeaths.grid(row=4,column=0)
total_deaths = total[1]
lbdat2 = Label(win,text=total_deaths.text)
lbdat2.grid(row=4,column=1)
lbcase = Label(win,text='Recovered: ')
lbcase.grid(row=5,column=0)
total_recover = total[2]
lbdat3 = Label(win,text=total_recover.text)
lbdat3.grid(row=5,column=1)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "E:\Python\Python Projects Fun\covid_gui.py", line 34, in statno
death_cases = str(df.values['Total Cases'][lb2.cget('text')])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or
boolean arrays are valid indices
对不起,我可能想提取相应列和行中的值,并将该值作为标签放入tkinter中,但是由于表中包含数字之间的逗号,因此出现了上述错误,因此我应该怎么转换它们才能得到值?因为我不知道如何处理对应值中的逗号