[PYTHON] Web scraping for weather warning notifications.

Please let me know the alert on Twitter.

Motivation

I did it

I'm a high school student. The school will be closed due to an alarm. The pious time to check the alarms one by one on that poorly responsive NHK data broadcast is too exciting, so I would like you to be notified automatically on Twitter. Yes.

problem

Somehow, there is no alarm API at all. I found it when I was looking for it, but apparently it seems that I'm using a bubbly protocol called "Habusabu Pab Bab".

...

Web scraping !!

This is it !!

environment

It feels like it.

Soy sauce

# coding: UTF-8
#!/usr/bin/env python3

from urllib import request
from bs4 import BeautifulSoup
from datetime import datetime
import time
import twitter

OK = '33[92m'
WARN = '33[93m'
NG = '33[91m'
END_CODE = '33[0m'

api = twitter.Api(consumer_key='Oh your', consumer_secret='Secret', access_token_secret='I'll put it here')

#URL to access
url = "https://typhoon.yahoo.co.jp/weather/jp/warn/Something/number/"


def Getter(soup):
    Summaryarea = soup.findAll("div", class_="warnSummary_box")
    # print(Summaryarea[0].dl)
    Li = Summaryarea[0].dl.dd.ul.li
    # adv = Li.find("span",class_="icoAdvisory")#Alert notification
    adv = Li.find("span", class_="icoWarning")  #Notice
    if adv:
        # Yes
        # print("OK")
        return 1
    else:
        # No
        # print("NO")
        return 0


def Twit(state):
    nowtime = datetime.now()
    NowStr = str(nowtime.hour) + "Time" + str(nowtime.minute) + "Minutes now"
    Txt = ""
    if state:
        Txt = "\n It seems that an alarm has been issued to the city somehow. .. ..\n I'm not happy because I'm on school holidays! !!"
    else:
        Txt = "\n It seems that there is no warning in the city somehow.\n Stop the weird bad luck and go to school quickly."
    status = api.PostUpdate("[Weather warning automatic notification system]\n" + NowStr + Txt)
    print(status.text)


def Coloring(txt, code):
    return "\033[%sm%s\033[0m" % (code, txt)


def main():
    while 1:
        print("Now " + ntStr)
        print("Its time!")

        #Read
        html = request.urlopen(url).read()

        soup = BeautifulSoup(html, "html.parser")
        Twit(Getter(soup))
        if Getter(soup):
            #With an alarm
            print(Coloring("!!!!WARNING!!!!", "31"))
        else:
            #Don't get an alarm
            print(Coloring("ALLGREEN", "32"))
        time.sleep(60 * 3)


if __name__ == "__main__":
    main()

result

Screenshot_20170723_003600Edited.png

I can do it.

Good vibes.

Improvement points

Link this with Raspberry Pi to lock the house when an alarm is issued www

Recommended Posts

Web scraping for weather warning notifications.
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
web scraping
WEB scraping with Python (for personal notes)
[For beginners] Try web scraping with Python
web scraping (prototype)
Let's do web scraping with Python (weather forecast)
Beginners can use Python for web scraping (1) Improved version
Data analysis for improving POG 1 ~ Web scraping with Python ~
Introduction to Web Scraping
Python web scraping selenium
Beginners use Python for web scraping (4) --2 Scraping on Cloud Shell
Web scraping with python + JupyterLab
Web scraping notes in python3
Save images with web scraping
Web scraping technology and concerns
Trade-offs in web scraping & crawling
Web scraping using Selenium (Python)
Scraping weather forecast with python
Web scraping using AWS lambda
About mod_wsgi's "Compiled for ~" warning
[Python + Selenium] Tips for scraping
Web scraping beginner with python
Algorithm-based web scraping library Scrapely
One-liner web scraping by tse
Studying web scraping for the purpose of extracting data from Filmarks # 2