質問編集履歴
4
回答を受け修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
'user_id':tweetinfo[j]['user']['id'],
|
26
26
|
|
27
|
-
"
|
27
|
+
"screen_name":tweetinfo[j]["screen_name"]}
|
28
28
|
|
29
29
|
with open(output_csv_file_name, 'a') as f:
|
30
30
|
|
3
回答を受け、修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,42 @@
|
|
1
|
+
### 修正後のソースコード
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
```ここに
|
6
|
+
|
7
|
+
user_info = ["user.screen_name", "user.name",]
|
8
|
+
|
9
|
+
request_result = request_authorization(request_url=request_url, ck=CK, cs=CS, at=AT, ats=AS, params=params, user_info=user_info)
|
10
|
+
|
11
|
+
if request_result.status_code == 200:
|
12
|
+
|
13
|
+
tweetinfo = json.loads(request_result.text)
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
for j in range(0,len(tweetinfo)):
|
18
|
+
|
19
|
+
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']) + str(twitter_info[j]["screen_name"]))
|
20
|
+
|
21
|
+
user = api.get_user(j)
|
22
|
+
|
23
|
+
followerdata = {'retweet_id':tweetinfo[j]['id'],
|
24
|
+
|
25
|
+
'user_id':tweetinfo[j]['user']['id'],
|
26
|
+
|
27
|
+
"user_name":tweetinfo["screen_name"]}
|
28
|
+
|
29
|
+
with open(output_csv_file_name, 'a') as f:
|
30
|
+
|
31
|
+
writer = csv.writer(f, lineterminator='\n')
|
32
|
+
|
33
|
+
writer.writerow(followerdata.values())
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
1
|
-
### 前提・実現したいこと
|
39
|
+
```### 前提・実現したいこと
|
2
40
|
|
3
41
|
|
4
42
|
|
@@ -14,17 +52,17 @@
|
|
14
52
|
|
15
53
|
エラーメッセージ
|
16
54
|
|
17
|
-
line
|
55
|
+
line 63, in <module>
|
18
|
-
|
56
|
+
|
19
|
-
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']) +
|
57
|
+
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']) + str(tweetinfo[j]["screen_name"]))
|
20
|
-
|
58
|
+
|
21
|
-
|
59
|
+
KeyError: 'screen_name'
|
22
|
-
|
60
|
+
|
23
|
-
```
|
61
|
+
```
|
24
|
-
|
25
|
-
|
26
|
-
|
62
|
+
|
63
|
+
|
64
|
+
|
27
|
-
###
|
65
|
+
### 全体のソースコード
|
28
66
|
|
29
67
|
|
30
68
|
|
2
idのところにuser等をいれてみた
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,11 +14,11 @@
|
|
14
14
|
|
15
15
|
エラーメッセージ
|
16
16
|
|
17
|
-
line5
|
17
|
+
line59
|
18
|
-
|
18
|
+
|
19
|
-
r
|
19
|
+
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']) + user_info[j]["screen_name"])
|
20
|
-
|
20
|
+
|
21
|
-
|
21
|
+
TypeError: string indices must be integers
|
22
22
|
|
23
23
|
```
|
24
24
|
|
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
request_status_url = "https://api.twitter.com/1.1/statuses/retweets/:id.json"
|
80
80
|
|
81
|
-
tweet_id = [""] #リツイート情報を取得したい TweetID
|
81
|
+
tweet_id = ["1414972474261786636"] #リツイート情報を取得したい TweetID
|
82
82
|
|
83
83
|
|
84
84
|
|
@@ -138,7 +138,9 @@
|
|
138
138
|
|
139
139
|
params = {'tweet_id':tweet_id[i]}
|
140
140
|
|
141
|
+
user_info = ["user.screen_name", "user.name",]
|
142
|
+
|
141
|
-
request_result = request_authorization(request_url=request_url, ck=CK, cs=CS, at=AT, ats=AS, params=params, user_n
|
143
|
+
request_result = request_authorization(request_url=request_url, ck=CK, cs=CS, at=AT, ats=AS, params=params, user_info=user_info)
|
142
144
|
|
143
145
|
if request_result.status_code == 200:
|
144
146
|
|
@@ -146,11 +148,13 @@
|
|
146
148
|
|
147
149
|
for j in range(0,len(tweetinfo)):
|
148
150
|
|
149
|
-
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']))
|
151
|
+
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']) + user_info[j]["screen_name"])
|
150
152
|
|
151
153
|
followerdata = {'retweet_id':tweetinfo[j]['id'],
|
152
154
|
|
153
|
-
'user_id':tweetinfo[j]['user']['id'],
|
155
|
+
'user_id':tweetinfo[j]['user']['id'],
|
156
|
+
|
157
|
+
"user_name":user_info["screen_name"]}
|
154
158
|
|
155
159
|
with open(output_csv_file_name, 'a') as f:
|
156
160
|
|
@@ -186,10 +190,14 @@
|
|
186
190
|
|
187
191
|
```
|
188
192
|
|
193
|
+
```
|
194
|
+
|
189
195
|
|
190
196
|
|
191
197
|
### 試したこと
|
192
198
|
|
199
|
+
コードが読みにくくて申し訳ございません。
|
200
|
+
|
193
201
|
webで掲載されていたコード使用しました。
|
194
202
|
|
195
203
|
取得情報を「ID」ではなく「screen_name」に変えて情報を取得したかったのですがエラーが出てしまいます。
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,6 +14,12 @@
|
|
14
14
|
|
15
15
|
エラーメッセージ
|
16
16
|
|
17
|
+
line54
|
18
|
+
|
19
|
+
request_result = request_authorization(request_url=request_url, ck=CK, cs=CS, at=AT, ats=AS, params=params, user_name=screen_name)
|
20
|
+
|
21
|
+
NameError: name 'screen_name' is not defined
|
22
|
+
|
17
23
|
```
|
18
24
|
|
19
25
|
|
@@ -26,21 +32,17 @@
|
|
26
32
|
|
27
33
|
ソースコード
|
28
34
|
|
29
|
-
```
|
35
|
+
```from requests_oauthlib import OAuth1Session
|
30
36
|
|
31
37
|
import json
|
32
38
|
|
33
39
|
import csv
|
34
40
|
|
41
|
+
import tweepy
|
42
|
+
|
35
|
-
import co
|
43
|
+
import configparser
|
36
|
-
|
37
|
-
|
44
|
+
|
38
|
-
|
39
|
-
|
45
|
+
|
40
|
-
|
41
|
-
OAuth認証
|
42
|
-
|
43
|
-
'''
|
44
46
|
|
45
47
|
def request_authorization (**url_and_params):
|
46
48
|
|
@@ -56,168 +58,134 @@
|
|
56
58
|
|
57
59
|
return request_result
|
58
60
|
|
59
|
-
|
61
|
+
|
60
|
-
|
61
|
-
|
62
|
+
|
62
|
-
|
63
|
-
'''
|
64
|
-
|
65
|
-
|
63
|
+
config_ini = configparser.ConfigParser()
|
66
|
-
|
64
|
+
|
67
|
-
|
65
|
+
config_ini.read('config.ini', encoding='utf-8')
|
68
|
-
|
69
|
-
|
66
|
+
|
70
|
-
|
71
|
-
print(str(sec)+"秒待機")
|
72
|
-
|
73
|
-
time.sleep(sec)
|
74
|
-
|
75
|
-
#取得した4つのkeyをここに差し込みます。
|
76
|
-
|
77
|
-
CK = "
|
67
|
+
CK = config_ini["key"]["consumer_key"]
|
78
|
-
|
68
|
+
|
79
|
-
CS = "
|
69
|
+
CS = config_ini["key"]["consumer_secret"]
|
80
|
-
|
81
|
-
|
70
|
+
|
82
|
-
|
83
|
-
A
|
71
|
+
AT = config_ini["key"]["access_token"]
|
72
|
+
|
84
|
-
|
73
|
+
AS = config_ini["key"]["access_secret"]
|
74
|
+
|
85
|
-
global twitter
|
75
|
+
global twitter
|
86
|
-
|
76
|
+
|
87
|
-
twitter = OAuth1Session(CK, CS, AT, AS)
|
77
|
+
twitter = OAuth1Session(CK, CS, AT, AS)
|
88
|
-
|
89
|
-
# ツイート情報取得用エンドポイントURL
|
90
78
|
|
91
79
|
request_status_url = "https://api.twitter.com/1.1/statuses/retweets/:id.json"
|
92
80
|
|
93
|
-
'''
|
94
|
-
|
95
|
-
情報を取得したいTweetIDをリストに格納
|
96
|
-
|
97
|
-
'''
|
98
|
-
|
99
|
-
tweet_id
|
81
|
+
tweet_id = [""] #リツイート情報を取得したい TweetID
|
100
|
-
|
101
|
-
|
82
|
+
|
102
|
-
|
83
|
+
|
84
|
+
|
103
|
-
total_requests_number = len(tweet_id
|
85
|
+
total_requests_number = len(tweet_id)
|
104
86
|
|
105
87
|
print("--------"+"送信するリクエスト数合計:" + str(total_requests_number)+"-------------")
|
106
88
|
|
107
|
-
'''
|
108
|
-
|
109
|
-
TwitterからUserID情報の取得開始
|
110
|
-
|
111
|
-
'''
|
112
|
-
|
113
|
-
sched_requests_number = 0
|
89
|
+
sched_requests_number = 0
|
114
|
-
|
90
|
+
|
115
|
-
should_get_requests_limit = True
|
91
|
+
should_get_requests_limit = True
|
116
|
-
|
92
|
+
|
117
|
-
i_plus = 1
|
93
|
+
i_plus = 1
|
118
|
-
|
94
|
+
|
119
|
-
requests_count = 0
|
95
|
+
requests_count = 0
|
120
|
-
|
96
|
+
|
121
|
-
error_count = 0
|
97
|
+
error_count = 0
|
122
|
-
|
98
|
+
|
123
|
-
output_csv_file_name = 'retweet_info.csv'
|
99
|
+
output_csv_file_name = 'retweet_info.csv'
|
124
|
-
|
125
|
-
|
100
|
+
|
126
|
-
|
127
|
-
|
101
|
+
|
128
|
-
|
129
|
-
'''
|
130
102
|
|
131
103
|
with open(output_csv_file_name,'a') as f:
|
132
104
|
|
133
105
|
writer = csv.writer(f, lineterminator='\n')
|
134
106
|
|
135
|
-
header = ['
|
107
|
+
header = ['retweet_id', 'user_id']
|
136
108
|
|
137
109
|
writer.writerow(header)
|
138
110
|
|
139
|
-
|
111
|
+
|
140
|
-
|
141
|
-
ループ開始。retweet_idの書き込み。
|
142
|
-
|
143
|
-
'''
|
144
112
|
|
145
113
|
for i in range(0,total_requests_number,i_plus):
|
146
114
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
should_get_requests_limit = True
|
115
|
+
if(should_get_requests_limit == True):
|
116
|
+
|
117
|
+
api_response = request_authorization(request_url=request_status_url, ck=CK, cs=CS, at=AT, ats=AS)
|
118
|
+
|
119
|
+
requests_limit = api_response.headers['x-rate-limit-remaining']
|
120
|
+
|
121
|
+
requests_limit = int(requests_limit)
|
122
|
+
|
123
|
+
requests_count += 1
|
124
|
+
|
125
|
+
print("--------"+str(requests_count)+"回目。今回送信するリクエスト数:" + str(requests_limit)+"--------------------")
|
126
|
+
|
127
|
+
sched_requests_number += requests_limit
|
128
|
+
|
129
|
+
should_get_requests_limit = False
|
130
|
+
|
131
|
+
if(requests_limit != 0):
|
132
|
+
|
133
|
+
i_plus = 1
|
134
|
+
|
135
|
+
print(str(i) + ":リクエストしたtweetID:" + str(tweet_id[i])+"---------")
|
136
|
+
|
137
|
+
request_url = "https://api.twitter.com/1.1/statuses/retweets/"+str(tweet_id[i])+".json?count=100&trim_user=false"
|
138
|
+
|
139
|
+
params = {'tweet_id':tweet_id[i]}
|
140
|
+
|
141
|
+
request_result = request_authorization(request_url=request_url, ck=CK, cs=CS, at=AT, ats=AS, params=params, user_name=screen_name)
|
142
|
+
|
143
|
+
if request_result.status_code == 200:
|
144
|
+
|
145
|
+
tweetinfo = json.loads(request_result.text)
|
146
|
+
|
147
|
+
for j in range(0,len(tweetinfo)):
|
148
|
+
|
149
|
+
print(str(i)+":"+str(j)+":"+"リツイートID" + str(tweetinfo[j]['id']))
|
150
|
+
|
151
|
+
followerdata = {'retweet_id':tweetinfo[j]['id'],
|
152
|
+
|
153
|
+
'user_id':tweetinfo[j]['user']['id'],}
|
154
|
+
|
155
|
+
with open(output_csv_file_name, 'a') as f:
|
156
|
+
|
157
|
+
writer = csv.writer(f, lineterminator='\n')
|
158
|
+
|
159
|
+
writer.writerow(followerdata.values())
|
160
|
+
|
161
|
+
else:
|
162
|
+
|
163
|
+
print ("Error: %d" % request_result.status_code)
|
164
|
+
|
165
|
+
error_count += 1
|
166
|
+
|
167
|
+
print("Error数:"+str(error_count))
|
168
|
+
|
169
|
+
else:
|
170
|
+
|
171
|
+
i_plus = 0
|
172
|
+
|
173
|
+
if(i == sched_requests_number):
|
174
|
+
|
175
|
+
print("--------"+"残りリクエスト数" + str(total_requests_number-requests_limit) + "-------------")
|
176
|
+
|
177
|
+
sleep_until_api_limit(api_response)
|
178
|
+
|
179
|
+
should_get_requests_limit = True
|
180
|
+
|
181
|
+
|
216
182
|
|
217
183
|
コード
|
218
184
|
|
219
185
|
```
|
220
186
|
|
187
|
+
```
|
188
|
+
|
221
189
|
|
222
190
|
|
223
191
|
### 試したこと
|