import datetime
import requests
from bs4 import BeautifulSoup
def scraping():
url = 'http://183.176.244.72/cgi/050_HQ_100_03.cgi?GID=050_HQ_100&UI=U777&SI=00000&DT=000000000000&DBDT=0000000000&MNU=1&DTO=-1&DN=0972900400025&KTM=3&GHK=3&YSK=0&SRO=1&LO=88&TD=0000&BTY=IE6X&cSessionID=0000000000000000000000&UIS=000000000000&SIMU=0&ZM=0'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html5lib')
data = []
for trs in soup.select('body > table > tbody > tr'):
temp = [i.get_text(strip=True) for i in trs.select('td.right-10')]
if temp[0]:
day = temp[0]
else:
temp[0] = day
today = datetime.datetime.now()
if temp[1] == '24:00':
hiduke = datetime.datetime.strptime(
str(today.year) + '/' + temp[0] + ' ' + '00:00',
'%Y/%m/%d %H:%M')
hiduke += datetime.timedelta(days=1)
else:
hiduke = datetime.datetime.strptime(
str(today.year) + '/' + temp[0] + ' ' + temp[1],
'%Y/%m/%d %H:%M')
if hiduke > today:
hiduke -= datetime.timedelta(years=1)
try:
water_level = float(temp[2])
except:
pass
else:
data.append([hiduke, water_level])
return (data[-1][0], data[-1][1], data[-7][1])
if __name__ == '__main__':
hiduke, wl_now, wl_hour = scraping()
print('{}現在の水位は{}mです。'.format(hiduke.strftime('%Y/%m/%d %H:%M'), wl_now))
if wl_now > wl_hour:
if wl_now >= 2.85:
print('はん濫危険水位')
elif wl_now >= 2.60:
print('避難判断水位')
elif wl_now >= 2.40:
print('はん濫注意水位')
elif wl_now >= 2.10:
print('水防団待機水位')