teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

修正

2020/10/28 09:53

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -11,4 +11,35 @@
11
11
  のように「forecast」を「weather」に直して実行してみてください。
12
12
 
13
13
  ( print(json.dumps(data, indent=1)の行は、デバッグのために追加したものなので、
14
- 削除してかまいません)
14
+ 削除してかまいません)
15
+
16
+
17
+ 全文:
18
+ ```
19
+ import requests
20
+
21
+ import json
22
+
23
+ apikey = "MMM"
24
+
25
+ cities = ["London,UK", "Tokyo,JP", "New York,US"]
26
+
27
+ api = "http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={key}"
28
+
29
+ k2c =lambda k: k - 273.15
30
+
31
+ for name in cities:
32
+ print("London,UK", "Tokyo,JP", "New York,US")
33
+
34
+ url = api.format(city=name, key=apikey)
35
+ r = requests.get(url)
36
+ data = json.loads(r.text)
37
+ print("+ 都市 =", data["name"])
38
+ print("| 天気 =", data["weather"][0]["description"])
39
+ print("| 最低気温 =", k2c(data["main"]["temp_min"]))
40
+ print("| 最高気温 =", k2c(data["main"]["temp_max"]))
41
+ print("| 湿度 =", data["main"]["humidity"])
42
+ print("| 気圧 =", data["main"]["pressure"])
43
+ print("| 風速度 =", data["wind"]["speed"])
44
+ print("")
45
+ ```

2

修正

2020/10/28 09:53

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -1,4 +1,4 @@
1
- 実際に自分でuopenweatherのアカウントをつくってログインして調べて見ました。
1
+ 実際に自分でopenweatherのアカウントをつくってログインして調べて見ました。
2
2
 
3
3
  元の"http://api.openweathermap.org/data/2.5/forecast?q={city}&APPID={key}"
4
4
  だと、データ構造が異なるため、うまく表示できません。

1

修正

2020/10/28 09:51

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -1,4 +1,5 @@
1
- 実際にログインして調べて見ました。
1
+ 実際に自分でuopenweatherのアカウントをつくってログインして調べて見ました。
2
+
2
3
  元の"http://api.openweathermap.org/data/2.5/forecast?q={city}&APPID={key}"
3
4
  だと、データ構造が異なるため、うまく表示できません。
4
5
 
@@ -7,6 +8,7 @@
7
8
  api = "http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={key}"
8
9
  ```
9
10
 
10
- のように「forecast」を「weather」に直してみてください。
11
+ のように「forecast」を「weather」に直して実行してみてください。
11
12
 
12
- ( print(json.dumps(data, indent=1)の行は削除てかまいません)
13
+ ( print(json.dumps(data, indent=1)の行は、デバッグのために追加たものなので、
14
+ 削除してかまいません)