As the title suggests, I made a weather forecast bot-like with Python (not a bot). I was thinking "I wish I could send it on LINE" because I was tired of checking the weather forecast, but my predecessors had already done it, so I made it with wisdom (almost plagiarism). Saw.
・ Get Yahoo weather information by scraping ・ Display the information obtained by scraping with LINE Notify
$pip install beautifulsoup4
$pip install requests
Issue a token with LINE Notify.
import urllib.request
import requests
from bs4 import BeautifulSoup
line_notify_token = 'xxxxxxxxxxxxxxxxxxxxx'#Use the issued token.
line_notify_api = 'https://notify-api.line.me/api/notify'
rssurl = "https://rss-weather.yahoo.co.jp/rss/days/3410.xml"#This code gets the weather information for Sendai.
URL = "https://weather.yahoo.co.jp/weather/jp/8/3410/8201.html"
tenki = []
detail = []
def Parser(rssurl):
with urllib.request.urlopen(rssurl) as res:
xml = res.read()
soup = BeautifulSoup(xml, "html.parser")
for item in soup.find_all("item"):
title = item.find("title").string
description = item.find("description").string
if title.find("[ PR ]") == -1:
tenki.append(title)
detail.append(description)
def Otenki():
Parser(rssurl)
for i in range(0,2):
message = tenki[i]
payload = {'message': "\n" + message}
headers = {'Authorization': 'Bearer ' + line_notify_token}
line_notify = requests.post(line_notify_api, data=payload, headers=headers)
Otenki()
It made me happy that it was sent firmly.
Actually I wanted to do automation using AWS, Heroku, but I can not handle it if I jump in without any knowledge and receive a high bill, so I left it so far this time lol. You may try installing the schedule library. It was fun to be able to do various things without knowing when I tried to move it myself.
Also, I would like to update this article while acquiring skills.
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python LINE notification of weather forecast in Python
Recommended Posts