質問編集履歴

3

修正しました。

2020/03/27 01:36

投稿

ITOMO5963
ITOMO5963

スコア98

test CHANGED
File without changes
test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
  Traceback (most recent call last):
74
74
 
75
- File "c:/Users/itoutmv.TNC/Documents/Pythonファイル/Get-Monitoring-01.py", line 31, in <module>
75
+ File "c:/Users/Pythonファイル/Get-API.py", line 31, in <module>
76
76
 
77
77
  main()
78
78
 

2

回答をもとに修正しましあたが、上手くいきませんでした。

2020/03/27 01:36

投稿

ITOMO5963
ITOMO5963

スコア98

test CHANGED
File without changes
test CHANGED
@@ -8,106 +8,82 @@
8
8
 
9
9
 
10
10
 
11
- api.py
11
+ コード
12
12
 
13
13
  ```
14
14
 
15
- import requests
15
+ import os
16
16
 
17
+ import requests #POSTリクエストを送信する
18
+
17
- import pprint
19
+ import json
18
20
 
19
21
 
20
22
 
23
+ os.environ['HTTP_PROXY']
24
+
25
+ os.environ['HTTPS_PROXY']
26
+
27
+
28
+
21
29
  url = 'API URL'
22
30
 
23
- api = 'APIキー'
31
+ api = 'API キー'
24
32
 
25
33
 
26
34
 
27
- headers = {'API-KEY' : api}
35
+ headers = {'X-API-KEY' : api}
28
36
 
29
37
 
30
38
 
31
39
  def main():
32
40
 
33
- response = requests.get(url, headers=headers)
41
+ response = requests.get(url, headers=headers)
42
+
43
+ print(response.json())
44
+
45
+ x = json.loads(response.json())
46
+
47
+ for m in x["monitors"]:
48
+
49
+ if m["name"]=="hoge2_hoge2":
50
+
51
+ print(m["id"])
34
52
 
35
53
 
36
54
 
37
- pprint.pprint(response.json())
38
55
 
39
56
 
40
57
 
41
- if name== 'main':
42
58
 
59
+ if __name__== '__main__':
60
+
43
- main()
61
+ main()
44
62
 
45
63
  ```
46
64
 
47
65
 
48
66
 
49
-
50
-
51
67
  【出力結果】
52
68
 
53
- ```
69
+ ````
54
70
 
55
- {
71
+ {'monitors': [{'duration': 3, 'maxCheckAttempts': 1, 'critical': 55555, 'isMute': False, 'metric': 'custom.ping-A01.2_2_3_1', 'excludeScopes': [], 'name': 'hoge2_hoge2', 'warning': 5, 'id': '3Tn9vU7tGFS', 'scopes': [], 'type': 'host', 'operator': '>'}, {'duration': 3, 'maxCheckAttempts': 2, 'critical': 1500, 'isMute': False, 'metric': 'custom.ping-H15.2_2_9_150', 'excludeScopes': [], 'name': 'custom.ping-H15.2_2_9_150', 'warning': 200, 'id': '3QXEZcrXFMb', 'scopes': [], 'type': 'host', 'operator': '>'}, {'duration': 3, 'maxCheckAttempts': 2, 'critical': 1500, 'isMute': False, 'metric': 'custom.ping-H16.2_2_9_151', 'excludeScopes': [], 'name': 'custom.ping-H16.2_2_9_151', 'warning': 200, 'id': '3QXJNu7GjKs', 'scopes': [], 'type': 'host', 'operator': '>'}, {'critical': 1900, 'isMute': False, 'excludeScopes': [], 'memo': '回線品目:フレッツ光ネクスト\u3000マンション・ハイスピードタイプ\n回線ID:L5435-46841-7781', 'type': 'host', 'operator': '>', 'notificationInterval': 10, 'duration': 3, 'maxCheckAttempts': 2, 'metric': 'custom.ping-A01.2_2_3_1', 'name': '帯広支店ルータ死活監視', 'warning': 200, 'id': '3RWjpKzPrkE', 'scopes': []}, {'isMute': False, 'excludeScopes': [], 'name': 'connectivity', 'id': '3QPXmoYtNm9', 'scopes': [], 'type': 'connectivity'}]}
56
72
 
57
- "monitors": [
73
+ Traceback (most recent call last):
58
74
 
59
- {
75
+ File "c:/Users/itoutmv.TNC/Documents/Pythonファイル/Get-Monitoring-01.py", line 31, in <module>
60
76
 
61
- "id" : "123aqwde",
77
+ main()
62
78
 
63
- "type": "host",
79
+ File "c:/Users/Pythonファイル/Get-API.py", line 17, in main
64
80
 
65
- "name": "hoge_hoge",
81
+ x = json.loads(response.json())
66
82
 
67
- "memo": "demo test",
83
+ File "C:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 341, in loads
68
84
 
69
- "duration": 3,
85
+ raise TypeError(f'the JSON object must be str, bytes or bytearray, '
70
86
 
71
- "metric": "disk.cpu",
72
-
73
- "operator": ">",
74
-
75
- "warning": 20000.0,
76
-
77
- "critical": 400000.0,
78
-
79
- "maxCheckAttempts": 3
80
-
81
- },
82
-
83
- {
84
-
85
- "id" : "fslknfklns",
86
-
87
- "type": "host",
88
-
89
- "name": "ahoge2_hoge2",
90
-
91
- "memo": "demo test",
92
-
93
- "service": "Blog",
94
-
95
- "duration": 1,
96
-
97
- "metric": "access_num.4xx_count",
87
+ TypeError: the JSON object must be str, bytes or bytearray, not dict
98
-
99
- "operator": ">",
100
-
101
- "warning": 50.0,
102
-
103
- "critical": 100.0,
104
-
105
- "maxCheckAttempts": 1
106
-
107
- }
108
-
109
- ]
110
-
111
- }
112
88
 
113
89
  ```

1

ご指摘ありがとう御座います。修正させていただきました

2020/03/27 01:34

投稿

ITOMO5963
ITOMO5963

スコア98

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  以下のpythonスクリプトを実行。APIで情報を取得している。
4
4
 
5
- 出力結果"name"が"hoge2hoge2"をIDのみを取得したい。
5
+ 出力結果"name"が"hoge2_hoge2"をIDのみを取得したい。(フィルタ?)
6
6
 
7
7
  お力沿いお願いいたします。
8
8
 
9
9
 
10
10
 
11
- ***【api.py】***
11
+ 【api.py】
12
+
13
+ ```
12
14
 
13
15
  import requests
14
16
 
@@ -40,21 +42,15 @@
40
42
 
41
43
  main()
42
44
 
45
+ ```
43
46
 
44
47
 
45
48
 
46
49
 
47
50
 
51
+ 【出力結果】
48
52
 
49
- ***【出力結果】***
53
+ ```
50
-
51
- {'monitors': [{'id': 'ABC123DEF',
52
-
53
- 'metric': 'hogehoge',
54
-
55
- 'name': 'demodemo'}]}
56
-
57
-
58
54
 
59
55
  {
60
56
 
@@ -62,17 +58,17 @@
62
58
 
63
59
  {
64
60
 
65
- "id" : "123abc456",
61
+ "id" : "123aqwde",
66
62
 
67
63
  "type": "host",
68
64
 
69
- "name": "hogehoge",
65
+ "name": "hoge_hoge",
70
66
 
71
- "memo": "This monitor is hogehoge",
67
+ "memo": "demo test",
72
68
 
73
69
  "duration": 3,
74
70
 
75
- "metric": "disk.hoge",
71
+ "metric": "disk.cpu",
76
72
 
77
73
  "operator": ">",
78
74
 
@@ -80,30 +76,38 @@
80
76
 
81
77
  "critical": 400000.0,
82
78
 
83
- "maxCheckAttempts": 3,
79
+ "maxCheckAttempts": 3
84
80
 
85
81
  },
86
82
 
87
83
  {
88
84
 
89
- "id" : "987zxv587",
85
+ "id" : "fslknfklns",
90
86
 
91
87
  "type": "host",
92
88
 
93
- "name": "hoge2hoge2",
89
+ "name": "ahoge2_hoge2",
94
90
 
95
- "memo": "This monitor is hoge2hoge2",
91
+ "memo": "demo test",
96
92
 
97
- "duration": 3,
93
+ "service": "Blog",
98
94
 
95
+ "duration": 1,
96
+
99
- "metric": "disk.hoge2",
97
+ "metric": "access_num.4xx_count",
100
98
 
101
99
  "operator": ">",
102
100
 
103
- "warning": 20000.0,
101
+ "warning": 50.0,
104
102
 
105
- "critical": 400000.0,
103
+ "critical": 100.0,
106
104
 
107
- "maxCheckAttempts": 3,
105
+ "maxCheckAttempts": 1
108
106
 
109
107
  }
108
+
109
+ ]
110
+
111
+ }
112
+
113
+ ```