質問編集履歴
1
変更点追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,34 +2,49 @@
|
|
2
2
|
ご指摘、ご教授お願いします。
|
3
3
|
誤解で、通っていたと勘違いしてしまい。解決しまったのですが、エラーとなっていたため再度質問の途中から記載させていただきております。
|
4
4
|
よろしくお願い致します。
|
5
|
+

|
6
|
+
![終わり(ffbbf2b4299e69dfd776a2fd48221a05.png)
|
7
|
+
|
8
|
+
エラー内容。
|
5
9
|
```python
|
6
|
-
import requests
|
7
|
-
import json
|
8
|
-
↓
|
9
|
-
apikey = "MMM"
|
10
|
-
↓
|
11
|
-
cities = ["London,UK", "Tokyo,JP", "New York,US"]
|
12
|
-
↓
|
13
|
-
api = "http://api.openweathermap.org/data/2.5/forecast?q={city}&APPID={key}"
|
14
|
-
↓
|
15
|
-
k2c =lambda k: k - 273.15
|
16
|
-
↓
|
17
10
|
for name in cities:
|
18
11
|
print("London,UK", "Tokyo,JP", "New York,US")
|
12
|
+
|
13
|
+
url = api.format(city=name, key=apikey)
|
14
|
+
r = requests.get(url)
|
15
|
+
data = json.loads(r.text)
|
16
|
+
print(json.dumps(data, indent=1))
|
17
|
+
|
18
|
+
print("+ 都市 =", data["name"])
|
19
|
+
print("| 天気 =", data["weather"][0]["description"])
|
20
|
+
print("| 最低気温 =", k2c(data["main"]["temp_min"]))
|
21
|
+
print("| 最高気温 =", k2c(data["main"]["temp_max"]))
|
22
|
+
print("| 湿度 =", data["main"]["humidity"])
|
23
|
+
print("| 気圧 =", data["main"]["pressure"])
|
24
|
+
print("| 風速度 =", data["wind"]["speed"])
|
25
|
+
print("")
|
19
26
|
↓
|
27
|
+
KeyError Traceback (most recent call last)
|
28
|
+
<ipython-input-212-c7e14fb73807> in <module>
|
20
|
-
|
29
|
+
7 print(json.dumps(data, indent=1))
|
30
|
+
8
|
31
|
+
----> 9 print("+ 都市 =", data["name"])
|
21
|
-
|
32
|
+
10 print("| 天気 =", data["weather"][0]["description"])
|
33
|
+
11 print("| 最低気温 =", k2c(data["main"]["temp_min"]))
|
34
|
+
|
35
|
+
KeyError: 'name'
|
22
36
|
```
|
23
|
-
|
37
|
+
print("+ 都市 =", data["name"])無し。
|
24
38
|
```python
|
25
39
|
for name in cities:
|
26
40
|
print("London,UK", "Tokyo,JP", "New York,US")
|
27
41
|
|
28
42
|
url = api.format(city=name, key=apikey)
|
29
43
|
r = requests.get(url)
|
30
|
-
data = json.loads(r.
|
44
|
+
data = json.loads(r.text)
|
31
|
-
print(json.dumps(data, indent=1))
|
45
|
+
print(json.dumps(data, indent=1))
|
32
|
-
|
46
|
+
|
47
|
+
|
33
48
|
print("| 天気 =", data["weather"][0]["description"])
|
34
49
|
print("| 最低気温 =", k2c(data["main"]["temp_min"]))
|
35
50
|
print("| 最高気温 =", k2c(data["main"]["temp_max"]))
|
@@ -38,18 +53,55 @@
|
|
38
53
|
print("| 風速度 =", data["wind"]["speed"])
|
39
54
|
print("")
|
40
55
|
↓
|
41
|
-
London,UK Tokyo,JP New York,US
|
42
56
|
---------------------------------------------------------------------------
|
43
|
-
|
57
|
+
KeyError Traceback (most recent call last)
|
44
|
-
<ipython-input-
|
58
|
+
<ipython-input-213-0229e683493e> in <module>
|
45
|
-
4 url = api.format(city=name, key=apikey)
|
46
|
-
|
59
|
+
8
|
60
|
+
9
|
47
|
-
---
|
61
|
+
---> 10 print("| 天気 =", data["weather"][0]["description"])
|
48
|
-
7 print(json.dumps(data, indent=1))
|
49
|
-
|
62
|
+
11 print("| 最低気温 =", k2c(data["main"]["temp_min"]))
|
63
|
+
12 print("| 最高気温 =", k2c(data["main"]["temp_max"]))
|
50
64
|
|
51
|
-
|
65
|
+
KeyError: 'weather'
|
52
66
|
```
|
53
67
|
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
```python
|
91
|
+
import requests
|
92
|
+
import json
|
93
|
+
↓
|
94
|
+
apikey = "MMM"
|
95
|
+
↓
|
96
|
+
cities = ["London,UK", "Tokyo,JP", "New York,US"]
|
97
|
+
↓
|
98
|
+
api = "http://api.openweathermap.org/data/2.5/forecast?q={city}&APPID={key}"
|
99
|
+
↓
|
54
|
-
|
100
|
+
k2c =lambda k: k - 273.15
|
101
|
+
↓
|
102
|
+
for name in cities:
|
55
|
-
|
103
|
+
print("London,UK", "Tokyo,JP", "New York,US")
|
104
|
+
↓
|
105
|
+
for name in cities:
|
106
|
+
print("London,UK", "Tokyo,JP", "New York,US")
|
107
|
+
```
|