LoginSignup
2
1

More than 1 year has passed since last update.

pythonで天気情報のスクレイピング

Last updated at Posted at 2021-10-23

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

気象情報のデータが必要になったので、「tenki.jp」さんから1時間ごとの温度や湿度やらのデータを取ってきた。

注意

・スクレイピングを行うためには、法律やWebサイト利用規約を守る。
 公式に提供されているAPIがあるなら、それを利用する方がよい。
・スクレイピングスキル向上を目的でコードを書いたが、
 不正に利用しない、攻撃実験をしないを守っています。
・一部(取ってくるデータの地域など)は〇など記載して伏せます。

環境

pythonで仮想環境内、beautifulsoup4をインストール(pip、anaconda両方で試しました)。
両方とも仮想環境を作成して行うのがオススメです。

pip install beautifulsoup4


pip install requests

または

conda install beautifulsoup4


conda install requests

htmlのクラス

chromeで調べたら下記のようになっていた

時間:hour
気温:temperature
降水確率:prob-precip
降水量:precipitation
湿度:humidity
風速:wind-speed

コード

test.py
#-*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup

//tenki.jpの1時間ごとの天気予報のURL(〇は伏せています)
url = "https://tenki.jp/forecast/〇/〇/〇/〇/1hour.html"

r = requests.get(url)

bsobject = BeautifulSoup(r.content,"html.parser")
town_1h = bsobject.find(class_="forecast-point-1h")

//時間
hour = town_1h.find(class_="hour")
h_span = hour.find_all("span")
h = [int(h_span[x].string) for x in range(len(h_span))]
print("時間(h)")
print("{}\n".format(h))

//気温
temp = town_1h.find(class_="temperature")
t_span = temp.find_all("span")
t = [float(t_span[x].string) for x in range(len(t_span))]
print("気温(度)")
print("{}\n".format(t))

//降水確率
rainy_p = town_1h.find(class_="prob-precip")
p_span = rainy_p.find_all("span")
p = [p_span[x].string for x in range(len(p_span))]
print("降水確率(%)")
print("{}\n".format(p))

//降水量
rainy_a = town_1h.find(class_="precipitation")
a_span = rainy_a.find_all("span")
a = [a_span[x].string for x in range(len(a_span))]
print("降水量(mm/h)")
print("{}\n".format(a))

//湿度
humidity = town_1h.find(class_="humidity")
h_span = humidity.find_all("span")
y = [h_span[x].string for x in range(len(h_span))]
print("湿度(%)")
print("{}\n".format(y))

//風速
wind_speed = town_1h.find(class_="wind-speed")
w_span = wind_speed.find_all("span")
w = [int(w_span[x].string) for x in range(len(w_span))]
print("風速(m/s)")
print("{}\n".format(w))

//下記のコードは得たデータをテキストに書き込むため

all_list = [h,t,p,a,y,w]

filename = "weather_town.txt"

with open(filename,"wt",encoding="utf-8") as f:
        f.write(str(all_list))
f.close()

出力結果

例:〇〇市のデータ

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

気温(度)
[10.0, 9.5, 9.5, 9.0, 8.5, 7.5, 9.5, 12.0, 15.0, 16.5, 18.0, 19.0, 19.0, 18.5, 18.5, 17.5, 15.9, 14.7, 13.6, 12.8, 12.0, 11.6, 11.2, 10.8]

降水確率(%)
['(%)', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '10', '0', '0', '0', '0', '0']

降水量(mm/h)
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']    

湿度(%)
['(%)', '78', '80', '80', '82', '84', '80', '76', '58', '46', '40', '34', '34', '32', '34', '36', '38', '42', '44', '46', '48', '48', '50', '52', '54']

風速(m/s)
[2, 2, 1, 2, 1, 0, 1, 1, 1, 4, 2, 3, 3, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2]

テキストデータは今回は乗せませんが、同じディレクトリには作成されています。

参考

Pythonによるスクレイピング&機械学習 開発テクニック
https://www.amazon.co.jp/dp/4802611927/ref=cm_sw_r_tw_dp_61JA9G18RWK957HW4P92

スクレイピング・ハッキング・ラボ
miraihack 著

2
1
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
2
1