1
2

More than 1 year has passed since last update.

LINE Notify で天気予報を通知する

Last updated at Posted at 2022-07-14

LINE Notify アクセストークンを発行する

次のページを参考に「アクセストークン」を取得する。

API を確認する

通知系

Python実装

token = "アクセストークン" のところは、先ほど取得した自身のアクセストークンに変える。

!pip install BeautifulSoup4
from bs4 import BeautifulSoup
import requests
import datetime
import locale

## 日付取得 
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
today_date = datetime.date.today()
tomorrow_date = today_date + datetime.timedelta(days=1)
today_date_format = "{0:%Y年%m月%d日}".format(today_date)
today_strftime = today_date.strftime('%a')
tomorrow_date_format = "{0:%Y年%m月%d日}".format(tomorrow_date)
tomorrow_strftime = tomorrow_date.strftime('%a')

## 天気のページを取得
weather_url = "https://weather.yahoo.co.jp/weather/jp/13/4410.html"
response = requests.get(weather_url)
html = BeautifulSoup(response.text, "html.parser")
forecastcity = html.find_all("div", attrs={"class":"forecastCity"})[0]

## 今日の天気
today = forecastcity.find_all("div")[0]
today_weather = today.find_all("p", attrs ={"class" :  "pict"})[0].text.replace("\n","").replace(" ","")
today_high = today.find_all("li")[0].text
today_low = today.find_all("li")[1].text
today.find_all("td")
today_rain_06 = today.find_all("td")[4].text
today_rain_12 = today.find_all("td")[5].text
today_rain_18 = today.find_all("td")[6].text
today_rain_24 = today.find_all("td")[7].text

## 明日の天気
tomorrow = forecastcity.find_all("div")[1]
tomorrow_weather = tomorrow.find_all("p", attrs ={"class" :  "pict"})[0].text.replace("\n","").replace(" ","")
tomorrow_high = tomorrow.find_all("li")[0].text
tomorrow_low = tomorrow.find_all("li")[1].text
tomorrow.find_all("td")
tomorrow_rain_06 = tomorrow.find_all("td")[4].text
tomorrow_rain_12 = tomorrow.find_all("td")[5].text
tomorrow_rain_18 = tomorrow.find_all("td")[6].text
tomorrow_rain_24 = tomorrow.find_all("td")[7].text

## 今日の天気のメッセージ
today_message="""
今日{}({})の天気は{}
最高気温は{}、最低気温は{}
降水確率は、0-6時:{}、6-12時:{}、12-18時:{}、18-24時:{}
""".format(today_date_format, today_strftime, today_weather, today_high, today_low, today_rain_06, today_rain_12, today_rain_18, today_rain_24)

## 明日の天気のメッセージ
tomorrow_message="""
明日{}({})の天気は{}
最高気温は{}、最低気温は{}
降水確率は、0-6時:{}、6-12時:{}、12-18時:{}、18-24時:{}
""".format(tomorrow_date_format, tomorrow_strftime, tomorrow_weather, tomorrow_high, tomorrow_low, tomorrow_rain_06, tomorrow_rain_12, tomorrow_rain_18, tomorrow_rain_24)

token = "アクセストークン"
url = "https://notify-api.line.me/api/notify"
auth = {"Authorization":"Bearer "+token}
content = {"message" : today_message + tomorrow_message}
requests.post(url, headers=auth, data=content)
1
2
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
1
2