** Get the warning information of the Japan Meteorological Agency and notify by Slack if there is a warning in the specified area **
This article What you want to do is Don Pisha! I thought that I was proceeding according to the article, but every time the data of the Japan Meteorological Agency is updated, I am running the task of Zapier (a tool that can automate the work) My plan exceeds the maximum task ...
So I narrowed down the scope of what I wanted to do.
** Get the warning information of the Japan Meteorological Agency at "7:00 every morning" and notify you with Slack if there is a warning in the designated area ** ▼ It looks like this (it looks like I got a warning instead because there was no alarm when I moved it) I got the information only once every morning at 7 o'clock and set the number of task executions to once a day.
I found out how to scrape and wrote it because I thought I had to study because what I wanted to do changed a little. First Python! When I was researching, I found out that I would use something called Beautiful Soup, but Zapier Help ) Wrote something like this. Oh, oh, I can't use Beautiful Soup! There is a feeling of force, but I will extract only the desired part (alarm part) without using Beautiful Soup.
import re
import requests
import time
code_list = [['Sapporo', '0110000'], ['Eastern Sendai City', '0410001'], ['Saitama City', '1110000']]
output = {'text': ''}
for code in code_list:
html = requests.get('https://www.jma.go.jp/jp/warn/f_' + code[1] + '.html').text
data_list = re.findall('<span style="color:#FF2800">(.*?)</span>', html)
if len(data_list) == 0:
continue
text = '【' + code[0] + '】'
i = 1
for data in data_list:
if i == len(data_list):
text += data
else:
data = re.sub('alarm', '', data)
text += data + ','
i += 1
output['text'] += text + '\n'
time.sleep(1)
Since the font color of the alarm part is # FF2800
on the page of each region, all the parts surrounded by<span style = "color: # FF2800"> ~ </ span>
are extracted.
Also, since it scrapes multiple pages, rest for 1 second with time.sleep (1)
.
Obtain information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo I was very helpful including how to use Zapier.
Recommended Posts