LoginSignup
32
38

More than 1 year has passed since last update.

【Python】ホームページに更新があったらLineへ通知するbotを作ってみた

Last updated at Posted at 2021-09-02

はじめに

息子を近場の人気の幼稚園に入れたいが、かなりの激戦区。

入園までのプロセスは
(1) 息子が2歳の10月頃に3歳が通える「プレ幼稚園入園権」をゲット★1
(2) 3歳の時「プレ幼稚園」に通う
(3) 3歳の10月頃に4歳から通える「幼稚園入園権」をゲット
(4) (3)から応募してきた家族とバトルになるが、「プレ幼稚園」通っている家族が優先
(5) ライバルより優先的に入園できる

そもそも2歳からバトルが始まっているのです・・・。

そして、★1「プレ幼稚園入園権」は幼稚園のHPのブログに「案内書」の配布の詳細が記載される。
いつ更新されるかはわからない。。毎日HPを確認しなければならないのです。
ということで、
【Python】ホームページに更新があったらLineへ通知するbotを作ってみました。

環境

・Windows10
・Python3.7

概要

1.幼稚園のHPのHTMLファイルを抽出するために、Beautiful SoupというPythonライブラリを利用します。
2.Windowsタスクスケジューラで毎日9時ごろに以下のプログラムを実行します。
3.今日の幼稚園のHPのHTMLと昨日の幼稚園のHPのHTMLファイルを比較し、更新があればLineNotifyで自分のLineに通知します。更新がなければLineでの通知はしません。

Windowsタスクスケジューラの設定方法は以下の記事をご参考下さい。

プログラム

HP.py
import requests
from bs4 import BeautifulSoup
import datetime
import os

#windowsのどこにhtmlを保存するか指定します。
#ご自身の好きな場所にフォルダを作り、指定してください。
#今回はCドライブ直下に'Python'というフォルダを作っています。
path = 'C:/Python/'


#abc幼稚園のHPからhtmlファイルを取得します。
#'http://abc.youchien.net/'を書き換えて使ってください。
data = requests.get('http://abc.youchien.net/')
with open('abcsite.html', 'w',encoding='utf_8_sig') as file:
    file.write(data.text)

#取得したhtmlファイル名を変更_yyyymmdd_abcsite.html
#これをしないと昨日とファイルとごっちゃになりますね。
#変更不要です。
now = datetime.datetime.now()
os.rename("abcsite.html", now.strftime("%Y%m%d") + "_abcsite.html")

#昨日のファイルを呼び出す
#変更不要です。
before = now - datetime.timedelta(days = 1 )
fp1='{0:%Y%m%d}_abcsite.html'.format(before)
filepath = str(path)+fp1

with open(filepath , encoding='utf_8_sig') as f:
    html = f.read()
soup = BeautifulSoup(html, 'lxml')

#本日のファイルを呼び出す
#変更不要です。
fp2='{0:%Y%m%d}_abcsite.html'.format(now)
filepath2 = str(path)+fp2
with open(filepath2 , encoding='utf_8_sig') as f:
    html = f.read()
    soup2 = BeautifulSoup(html, 'lxml')

#昨日のファイルと本日のファイルを比較して同じかどうかで
#処理を分けています。
#変更不要です。
b =soup == soup2

#比較して違いがあるか
#違いがない場合は終了します。
#変更不要です。
def if_abc(b):
    if b == True:
        print('ありません')
        os.remove(filepath)

#違いがある場合はLine API連携します。
#"abc幼稚園のHPで"と"http://abc.youchien.net/"は適宜変えて下さい。
#line_notify_token = '************************************'も
#自分のトークンに変更して下さい。line_notify_tokenの発行方法はググればすぐ出ます。

    else:
        addmessage="abc幼稚園のHPで" "\r\n" "情報の更新がありました。" "\r\n" "内容の確認をして下さい。" "\r\n" "http://abc.youchien.net/"
        line_notify_token = '************************************'
        line_notify_api = 'https://notify-api.line.me/api/notify'
        message = addmessage
        payload = {'message':"\r\n"+ message}
        headers = {'Authorization': 'Bearer ' + line_notify_token}  # 発行したトークン
        line_notify = requests.post(line_notify_api, data=payload, headers=headers)
        os.remove(filepath)
if_abc(b)



終わりに

Beautiful SoupというPythonライブラリは初めて使ったので美しくはないと思いますがやりたいことはできました。
どちらかというとWindowsタスクマネージャーが動かないことがあるかも・・・。
ご参考になれば幸いです。

32
38
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
38