我得到的错误是:
Traceback (most recent call last):
File "e:\AMAN\Projects\Python\CoronaVirus Outbreak Notification System\main2.py", line 28, in <module>
for tr in soup.find_all('tbody')[1].find_all('tr'):
IndexError: list index out of range
我正在开发这个程序,它将根据一个州内的时间(1小时或2小时后),实时更新新病例数、死亡人数和已恢复的冠状病毒病例数。它还将提供关于其中有多少人是印度国民和外国国民的信息。使用bs4的python代码如下所示:
from plyer import notification
import requests
from bs4 import BeautifulSoup
import time
def notifyMe(title, message):
notification.notify(
title = title,
message = message,
app_icon = "icon.ico",
timeout = 6
)
def getData(url):
r = requests.get(url)
return r.text
if __name__ == "__main__":
while True:
# notifyMe("Harry", "Lets stop the spread of this virus together")
myHtmlData = getData('https://www.mohfw.gov.in/')
soup = BeautifulSoup(myHtmlData, 'html.parser')
# print(soup.prettify())
myDataStr = ""
for tr in soup.find_all('tbody')[1].find_all('tr'):
myDataStr += tr.get_text()
myDataStr = myDataStr[1:]
itemList = myDataStr.split("\n\n")
states = ['Chandigarh', 'Telengana', 'Uttar Pradesh']
for item in itemList[0:22]:
dataList = item.split('\n')
if dataList[1] in states:
nTitle = 'Cases of Covid-19'
nText = f"State {dataList[1]}\nIndian : {dataList[2]} & Foreign : {dataList[3]}\nCured : {dataList[4]}\nDeaths : {dataList[5]}"
notifyMe(nTitle, nText)
time.sleep(2)
time.sleep(3600)
出现此错误是因为
soup.find_all('tbody')
没有索引1。时间不够长。如果您的代码有时只有那个索引,那么您可以使用相关问题 更多 >
编程相关推荐