見出し画像

Pythonの、外部データ引用方法

r = requests.get('http://spartacodingclub.shop/en/global_air/osaka')
rjson = r.json()

print(rjson)

今回は、Pythonの、外部データ引用の方法を学んだ。
JavaScriptのときは、fetchってものを使って、外部データを引っ張ってきたけど、パイソンではrequestsというライブラリで指示できるらしい。

r = requests.get('http://spartacodingclub.shop/en/global_air/osaka')
rjson = r.json()

forecasts = rjson['data']['forecast']

意味は、rjsonでひっぱってきたデータの中に、dataとforecastというのが入っています。これらを、forecastsと定義している。ってことかなぁ。

r = requests.get('http://spartacodingclub.shop/en/global_air/osaka')
rjson = r.json()
forecasts = rjson['data']['forecast']
for forecast in forecasts:
date = forecast['day']
ave = forecast['avg']
print (date,ave)

リクエストというパッケージを用いて、APIデータをもってきた。
日付と平均値を表示させてみた、という内容だった。

この記事が気に入ったらサポートをしてみませんか?