・ I always suffer from pollen from this time. ・ This year, there is corona, and people with hay fever are having a hard time. ・ Insufficient mask. ・ I want to make something using python, scraping, and heroku. ・ Output image ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Create a directory line_kafun on your desktop. Create main.py that handles line-sdk and weather.py that collects pollen information from Yahoo weather information.
The directory structure is as follows.
line_kafun
├main.py
├weather.py
├Procfile
├runtime.txt
└requirements.txt
Install the required packages.
pip install flask
pip install line-bot-sdk
pip install beautifulsopu4
pip install gunicorn
pip install lxml
pip install requests
(3)main.py
.py:main.py
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,LocationMessage
)
import os
import weather as wt #weather.Import py
app = Flask(__name__)
#Heroku environment variable settings
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["YOUR_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["YOUR_CHANNEL_SECRET"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
@app.route("/")
def hello_world():
return "hello world!"
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
if 'pollen' in event.message.text:
line_bot_api.reply_message(
event.reply_token,
[
TextSendMessage(text='What is your current location information?'),
TextSendMessage(text='https://line.me/R/nv/location/'),
]
)
@handler.add(MessageEvent, message=LocationMessage)
def handle_location(event):
text = event.message.address
result = wt.get_weather(text)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=result + '\uDBC0\uDC20')
)
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)
-For the address in the place of text = event.message.address, refer to [api reference] of line developers (https://developers.line.biz/ja/reference/messaging-api/#audio-message). -For the TextSendMessage part, I referred to the URL scheme of the LINE api reference. ・ ** The URL scheme can only be used for one-on-one talks, not for groups. ** ** ・'\ UDBC0 \ uDC20' is an emoji, and I referred to Use "LINE emoji" in LINE BOT. ..
(4)weather.py Scraping pollen information from Yahoo.
.py:weather.py
import requests
from bs4 import BeautifulSoup
import re
import lxml
def get_weather(text):
location = re.findall('\d{3}-\d{4}',text)
location2 = location[0].replace('-','')
url1 = "https://weather.yahoo.co.jp/weather/search/?p={}".format(location2)
url2 = ""
#Get the html information from the first url and get the second url from there
res = requests.get(url1)
res.encoding = res.apparent_encoding
html_doc = res.text
soup = BeautifulSoup(html_doc,"lxml")
content_1 = soup.find_all(id = 'rsltmuni')
for i in content_1:
content_2 = i.find('a')
url2 = 'https:' + content_2.get('href')
#Get today and tomorrow dates from the second url(today、nextday)
res = requests.get(url2)
res.encoding = res.apparent_encoding
html_doc = res.text
soup = BeautifulSoup(html_doc,"lxml")
content_3 = soup.find_all('p',class_='date')
today = content_3[0].get_text()
nextday = content_3[1].get_text()
#Get pollen status today and tomorrow
content_4 = soup.find_all('p',class_='flying')
today_kafun = content_4[0].get_text()
nextday_kafun = content_4[1].get_text()
#Get area
content_5 = soup.find_all('h2',class_='yjM')
area = content_5[0].get_text()
result = today + 'of' + area + 'Is' + '「{}」'.format(today_kafun) + 'That's right.' + '\n' +'\n'+ nextday + 'of' + area + 'Is' + '「{}」'.format(nextday_kafun) + 'That's right.' + '\n' +'\n' + 'be careful.'
return result
Finally, create the required files (requirements.txt, Procfile, runtime.txt) and deploy to Heroku. Since the deployment method is introduced on many sites, it is omitted. For details, refer to Making a bulletin board with Heroku, Flask, SQLAlchemy. Also, the LINE Developers Webhook settings are also introduced on many sites, so I will omit them. For details, refer to Making a LINE BOT.