質問編集履歴

4

書式の改善

2021/03/30 05:47

投稿

hoshiko
hoshiko

スコア3

test CHANGED
File without changes
test CHANGED
@@ -38,13 +38,13 @@
38
38
 
39
39
  headers = {
40
40
 
41
- 'X-ChatWorkToken': 'b0a4cbfe093f9ef0ae56d0b400ec1232',
41
+ 'X-ChatWorkToken': 'xxxxxxxxxxxxxxxxxx',
42
42
 
43
43
  }
44
44
 
45
45
 
46
46
 
47
- response = requests.get('https://api.chatwork.com/v2/rooms/213313301/messages?force=1', headers=headers)
47
+ response = requests.get('https://api.chatwork.com/v2/rooms/xxxxxxx/messages?force=1', headers=headers)
48
48
 
49
49
  print(response.status_code)
50
50
 

3

文法の修正

2021/03/30 05:47

投稿

hoshiko
hoshiko

スコア3

test CHANGED
File without changes
test CHANGED
@@ -52,12 +52,18 @@
52
52
 
53
53
  if response.status_code==200:
54
54
 
55
- data = response.json()
55
+ messages = response.json()
56
56
 
57
- with open('chat_log.json','a') as f:
57
+ with open('chat.log','a') as f:
58
58
 
59
- re.findall('[info].*[/info]', 'chat_log.json')
59
+ for message in messages:
60
60
 
61
- for i, dic in enumerate(data):
61
+ body = message['body']
62
62
 
63
+ for info in re.findall(r"[info]([\S\s]+)[/info]", body):
64
+
63
- f.write(dic['body'] + '\n')
65
+ f.write(info.key + '\n')
66
+
67
+ message.encode('cp932')
68
+
69
+ message.encode('cp932', "ignore")

2

修正

2021/03/30 04:53

投稿

hoshiko
hoshiko

スコア3

test CHANGED
File without changes
test CHANGED
@@ -54,38 +54,6 @@
54
54
 
55
55
  data = response.json()
56
56
 
57
- with open(re.findall('<info>.*</info>', 'chat_log.json'),'a') as f:
58
-
59
- for i, dic in enumerate(data):
60
-
61
- f.write(dic['body'] + '\n')import requests
62
-
63
- import json
64
-
65
- import re
66
-
67
-
68
-
69
- headers = {
70
-
71
- 'X-ChatWorkToken': 'b0a4cbfe093f9ef0ae56d0b400ec1232',
72
-
73
- }
74
-
75
-
76
-
77
- #cURLのオプション設定 {roomId}の箇所には取得したルームIDを入れる
78
-
79
- response = requests.get('https://api.chatwork.com/v2/rooms/213313301/messages?force=1', headers=headers)
80
-
81
- print(response.status_code)
82
-
83
-
84
-
85
- if response.status_code==200:
86
-
87
- data = response.json()
88
-
89
57
  with open('chat_log.json','a') as f:
90
58
 
91
59
  re.findall('[info].*[/info]', 'chat_log.json')

1

修正

2021/03/24 07:05

投稿

hoshiko
hoshiko

スコア3

test CHANGED
@@ -1 +1 @@
1
- 【python】チャットワークから<info>で囲まれたメッセージを取得したい
1
+ 【python】チャットワークから[info]で囲まれたメッセージを取得したい
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- チャットワークにて<info></info>で囲まれたメッセージを
3
+ チャットワークにて[info][/info]で囲まれたメッセージを
4
4
 
5
5
  データとして収集したいのですが、
6
6
 
@@ -58,4 +58,38 @@
58
58
 
59
59
  for i, dic in enumerate(data):
60
60
 
61
+ f.write(dic['body'] + '\n')import requests
62
+
63
+ import json
64
+
65
+ import re
66
+
67
+
68
+
69
+ headers = {
70
+
71
+ 'X-ChatWorkToken': 'b0a4cbfe093f9ef0ae56d0b400ec1232',
72
+
73
+ }
74
+
75
+
76
+
77
+ #cURLのオプション設定 {roomId}の箇所には取得したルームIDを入れる
78
+
79
+ response = requests.get('https://api.chatwork.com/v2/rooms/213313301/messages?force=1', headers=headers)
80
+
81
+ print(response.status_code)
82
+
83
+
84
+
85
+ if response.status_code==200:
86
+
87
+ data = response.json()
88
+
89
+ with open('chat_log.json','a') as f:
90
+
91
+ re.findall('[info].*[/info]', 'chat_log.json')
92
+
93
+ for i, dic in enumerate(data):
94
+
61
95
  f.write(dic['body'] + '\n')