Не находит нужный тег
Составить программу, которая читает прогноз погоды в заданном городе с сайта meteoprog.ua и сохраняет в файле Excel в отдельной строке текущую дату и прогнозы максимальной и минимальной температуры на каждый из следующих 5 дней. Запрос на погоду в заданном городе:
http://www.meteoprog.ua/ua/weather/<місто>/
Например,
http://www.meteoprog.ua/ua/weather/Kyiv/
from html.parser import HTMLParser
from urllib.request import urlopen
from urllib.parse import urlencode
from urllib.error import HTTPError
import datetime as D
import openpyxl
ENC = 'utf-8'
class WeatherViewParser(HTMLParser):
def __init__(self,*args, **kw):
super().__init__(*args, **kw)
self.done = False
self.in_p = False
self.span_count = 0
self.date=[]
self.in_span1=False
self.in_span2=False
self.temp=[]
self.found_div=False
self.information=[]
def handle_starttag(self, tag, attrs):
if not self.done:
if tag == 'span' and len(attrs)!=0:
for i in attrs:
if ('dayoffMonth' in i):
self.in_p = True
if tag=='span' and len(attrs)!=0 and attrs[0]==['class','tempDay floatL']:
self.found_div=True
if self.found_div and tag=='span' and attrs[0]==['class' ,'from']:
self.in_span1=True
if self.found_div and tag=='span' and attrs[0]==['class' ,'to'] :
self.in_span2=True
def handle_endtag(self, tag):
if not self.done:
if tag == 'span':
self.in_p = False
if tag=='span' and (len(self.temp)==5 or len(self.temp)>5) :
self.in_span1 = False
if tag=='span' and (len(self.temp)==5 or len(self.temp)>5):
self.in_span2 = False
def handle_data(self, data):
if not self.done:
if self.in_p and len(self.date)==8:
self.date.append(data)
print(self.date)
elif self.in_span1 and self.in_span2 and self.span_count!=1:
self.temp.append(data)
self.in_span1=False
self.in_span2=False
self.span_count+=1
if len(self.temp)==2:
self.create_list()
def create_list(self):
self.inform=self.date[0]+' '+self.date[1]
self.information.append((self.inform,self.temp[0],self.temp[1]))
self.temp = []
self.date = []
self.span_count = 0
if len(self.information)==5:
self.done=True
def get_date(self):
return self.date
def get_temp(self):
return self.temp
def get_information(self):
return self.information
class CityWeather:
def __init__(self, city):
self.city=city
self.inform=None
self.day=D.datetime.now().date().day
url = 'http://www.meteoprog.ua/'
path = {'': "ua/weather/{}/".format(city)}
url = url + urlencode(path, encoding=ENC)[1:]
try:
request = urlopen(url)
data = str(request.read(), encoding=ENC, errors='ignore')
Site = WeatherViewParser()
Site.feed(data)
self.inform=Site.get_information()
except HTTPError as e:
print(e)
def create_WB(self,file_name):
WB=openpyxl.load_workbook(file_name)
RSh = WB.create_sheet(self.city)
RSh.append([self.city])
RSh.append(['Day','Min_temp','Max_temp'])
for word in self.inform:
RSh.append([word[0],word[1],word[2]])
WB.save(file_name)
print('WELL DONE')
if __name__ == '__main__':
city1 = 'Kyiv'
city2='Volnogorsk'
file_name='Ind_26_1.xlsx'
City1 = CityWeather(city1)
City2=CityWeather(city2)
City1.create_WB(file_name)
City2.create_WB(file_name)