[Python] I asked LINE BOT to answer the weather forecast.

Introduction

I scraped tenki.jp and incorporated the weather forecast into linebot. https://tenki.jp/

environment

windows python 3.6.4

Rendering first

IMG_3332.jpg

First, prepare for scraping

    #Target site URL
    url = "https://tenki.jp/forecast/6/30/6200/27100/"
    #Instance creation
    res = urllib.request.urlopen(url)
    soup = BeautifulSoup(res, 'html.parser')

'html.parser' is like a filter needed for scraping.

Scraping by class

weather = soup.find_all("p", class_="weather-telop")
    temp = soup.find_all("dd", class_="high-temp temp")
    low_temp = soup.find_all("dd", class_="low-temp temp")
    tds = soup.select("tr.rain-probability td")
    hini = soup.find_all("h3", class_="left-style")

From the top, scraping is done in the order of "weather, temperature, minimum temperature, probability of precipitation, date". I need to get used to using find_all and select methods properly, but I thought that there would be no problem if I used only select.

In preparation for output

    tenki = hini[0].getText() + "\n\n" + weather[0].getText()
    kion = "\n best" + temp[0].getText()
    low_kion = "Minimum" + low_temp[0].getText()
    rain1 = "\n\n Precipitation probability\n00-06:00" + tds[0].getText()
    rain2 = "\n06-12 o'clock" + tds[1].getText()
    rain3 = "\n12-18:00" + tds[2].getText()
    rain4 = "\n18-24:00" + tds[3].getText()

All the scraped items will be obtained as a list. This time, basically, the content of [0] was today's data, and the content of [1] was tomorrow's data, so it was relatively easy.

Overall view

import urllib.request
from bs4 import BeautifulSoup

def getw():
    #Target site URL
    url = "https://tenki.jp/forecast/6/30/6200/27100/"
    #Instance creation
    res = urllib.request.urlopen(url)
    soup = BeautifulSoup(res, 'html.parser')
    #Target element
    #today's weather
    weather = soup.find_all("p", class_="weather-telop")
    temp = soup.find_all("dd", class_="high-temp temp")
    low_temp = soup.find_all("dd", class_="low-temp temp")
    tds = soup.select("tr.rain-probability td")
    hini = soup.find_all("h3", class_="left-style")


    tenki = hini[0].getText() + "\n\n" + weather[0].getText()
    kion = "\n best" + temp[0].getText()
    low_kion = "Minimum" + low_temp[0].getText()
    rain1 = "\n\n Precipitation probability\n00-06:00" + tds[0].getText()
    rain2 = "\n06-12 o'clock" + tds[1].getText()
    rain3 = "\n12-18:00" + tds[2].getText()
    rain4 = "\n18-24:00" + tds[3].getText()

    
    a = tenki+kion+low_kion+rain1+rain2+rain3+rain4
    return a

After that, load this function into main.py of linebot.

text_in = event.message.text

    if "today" in text_in:
        line_bot_api.reply_message(event.reply_token,TextSendMessage(text=scw.getw()))

When a user enters a character that contains the word "today", today's weather is displayed. Then deploy and complete! IMG_3332.jpg

Finally

I should be able to write smarter, so I will leave it as an issue next. Also, since it is a BOT that is not interesting at all, there are many things that can be done, such as adding arrangements such as "Have you got an umbrella?" If it rains, so let's leave this as the next task.

It doesn't matter at all, but my back hurts.

Recommended Posts

[Python] I asked LINE BOT to answer the weather forecast.
I want to know the weather with LINE bot feat.Heroku + Python
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
I want to send a message from Python to LINE Bot
Tweet the weather forecast with a bot
Tweet the weather forecast with a bot Part 2
[Python] I will upload the FTP to the FTP server.
I want to display the progress in Python!
I want to get the file name, line number, and function name in Python 3.4
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to graph the packages installed in Python
[Python] Try to read the cool answer to the FizzBuzz problem
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
I made a LINE BOT with Python and Heroku
[Python] I tried to graph the top 10 eyeshadow rankings
I want to write in Python! (3) Utilize the mock
I tried to solve the problem with Python Vol.1
I felt that I ported the Python code to C ++ 98.
The easiest line bot in the world to lose weight
I want to use the R dataset in python
I tried to notify the honeypot report on LINE
I tried to summarize the string operations of Python
I tried to put out the frequent word ranking of LINE talk with Python
Add a function to tell the weather of today to slack bot (made by python)
I tried to find the entropy of the image with python
I want to initialize if the value is empty (python)
I tried to simulate how the infection spreads with Python
[Part 1] Use Deep Learning to forecast the weather from weather images
[Part 3] Use Deep Learning to forecast the weather from weather images
I want to save the photos sent by LINE to S3
I tried to notify the train delay information with LINE Notify
maya Python I want to fix the baked animation again.
What I did to welcome the Python2 EOL with confidence
I tried to display the time and today's weather w
[Python] I tried to visualize the follow relationship of Twitter
[Part 2] Use Deep Learning to forecast the weather from weather images
I didn't know how to use the [python] for statement
Scraping weather forecast with python
I tried to implement the mail sending function in Python
I want to know the features of Python and pip
I tried to enumerate the differences between java and python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I downloaded the python source
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
I tried to make my own high school girl BOT with Rinna style with LINE BOT (Python & Heroku)
I tried to summarize the contents of each package saved by Python pip in one line
[Python] I tried to make a simple program that works on the command line using argparse.
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
[Python] I made a Line bot that randomly asks English words.
I tried to solve the ant book beginner's edition with python
[Introduction to Python] I compared the naming conventions of C # and Python.
I want to output the beginning of the next month with Python
I want to run the Python GUI when starting Raspberry Pi
I wrote the code to write the code of Brainf * ck in python
I tried to display the video playback time (OpenCV: Python version)
I wanted to solve the ABC164 A ~ D problem with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】