LoginSignup
4
10

More than 3 years have passed since last update.

天気予報をpythonでスクレイピング

Last updated at Posted at 2020-11-02

天気予報のスクレイピング

はじめに

自作しているIoTシステムで天気予報が必要になりました。

BeautifulSoupを使ってtenki.jpから草津市の時間と降水確率を取得しました。

環境

pythonではbeautifulsoup4が必要になります。

conda install beautifulsoup4 requests -y

スクレイピング

Chromeで検証をして、関わる部分のクラスやタグを調査。
毎時の降水確率が欲しかったので調べてみると、

class = forecast-point-1h
class = prob-precip
span の中にデータがあった。

そのため、取り出し方は以下のコードになる。

scraping

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

import requests
from bs4 import BeautifulSoup


#tenki.jpの目的の地域のページのURL(滋賀県草津市)
url = 'https://tenki.jp/forecast/3/16/4410/13208/'

#HTTPリクエスト
r = requests.get(url)

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

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

## 時間
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)

## 降水確率
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)
実行結果
[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']

参考

webスクレイピングで今日の天気と気温を取得 - Qiita

4
10
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
4
10