Scraping weather forecast with python

Weather forecast scraping

Introduction

I needed a weather forecast for my own IoT system.

I used BeautifulSoup to get the time and probability of precipitation in Kusatsu from tenki.jp.

environment

python requires beautifulsoup4.

conda install beautifulsoup4 requests -y

Scraping

Investigate the classes and tags of the related parts by verifying with Chrome. I wanted the probability of precipitation every hour, so I looked it up.

class = forecast-point-1h class = prob-precip There was data in the span.

Therefore, the extraction method is as follows.

scraping



# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup


#tenki.URL of the page of the target area of jp(Kusatsu City, Shiga Prefecture)
url = 'https://tenki.jp/forecast/3/16/4410/13208/'

#HTTP request
r = requests.get(url)

bsObj = BeautifulSoup(r.content, "html.parser")

kusatu_1h = bsObj.find(class_="forecast-point-1h")

##time
h = []
hour = kusatu_1h.find(class_="hour")
h_tem = hour.find_all('span')
h = [int(h_tem[x].string) for x in range(len(h_tem))]
print(h)

##rainy percent
kousui = kusatu_1h.find(class_="prob-precip")
k_tem = kousui.find_all('span')
k = [k_tem[x].string for x in range(len(k_tem))]
print(k)

Execution result


[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

['(%)', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '90', '90', '70', '70', '70', '80', '70']

reference

Get today's weather and temperature by web scraping --Qiita

Recommended Posts

Scraping weather forecast with python
Let's do web scraping with Python (weather forecast)
Scraping with Python
Scraping with Python
Get weather information with Python & scraping
Scraping with Python (preparation)
Try scraping with Python.
Scraping with Python + PhantomJS
Scraping with Selenium [Python]
Scraping with Python + PyQuery
Scraping RSS with Python
I made a weather forecast bot-like with Python.
I tried scraping with Python
Web scraping with python + JupyterLab
Scraping with Selenium + Python Part 1
Scraping with chromedriver in python
Festive scraping with Python, scrapy
Scraping with Selenium in Python
Scraping with Tor in Python
Scraping with Selenium + Python Part 2
I tried scraping with python
Web scraping beginner with python
[Scraping] Python scraping
Try scraping with Python + Beautiful Soup
Scraping with Node, Ruby and Python
Web scraping with Python ① (Scraping prior knowledge)
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
Web scraping with Python First step
I tried web scraping with python.
Scraping with Python and Beautiful Soup
Get the weather with Python requests
Get the weather with Python requests 2
Let's do image scraping with Python
Get Qiita trends with Python scraping
"Scraping & machine learning with Python" Learning memo
Tweet the weather forecast with a bot
Get property information by scraping with python
Python scraping notes
Scraping with selenium
FizzBuzz with Python3
Python Scraping get_ranker_categories
Scraping with selenium ~ 2 ~
Statistics with python
Python with Go
Automate simple tasks with Python Part1 Scraping
Getting Started with Python Web Scraping Practice
Twilio with Python
Python Scraping eBay
Play with 2016-Python
Tested with Python
Scraping with Selenium
[Personal note] Web page scraping with python3
Web scraping with Python ② (Actually scraping stock sites)
Horse Racing Site Web Scraping with Python
Python Scraping get_title
with syntax (Python)
Getting Started with Python Web Scraping Practice
Python: Scraping Part 1
Try HTML scraping with a Python library
Bingo with python