質問編集履歴
3
いただいた回答から修正を加え、コードを書き直した
test
CHANGED
File without changes
|
test
CHANGED
@@ -341,3 +341,145 @@
|
|
341
341
|
Jupiterで実行しています。
|
342
342
|
|
343
343
|
どなたか力をお貸しください。
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
以下jeanbiego様が回答してくださったものから修正したverです。
|
348
|
+
|
349
|
+
変更したdef search_tweetsの所のみ掲載します。
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
```python
|
354
|
+
|
355
|
+
def search_tweets(CK, CKS, AT, ATS, user_id, tweet_id, count, range):
|
356
|
+
|
357
|
+
# 文字列設定
|
358
|
+
|
359
|
+
user_id += ' exclude:retweets' # RTは除く
|
360
|
+
|
361
|
+
user_id = urllib.parse.quote_plus(user_id)
|
362
|
+
|
363
|
+
# リクエスト
|
364
|
+
|
365
|
+
url = "https://api.twitter.com/1.1/search/tweets.json?lang=ja&q="+user_id+"&count="+str(count)
|
366
|
+
|
367
|
+
auth = OAuth1(CK, CKS, AT, ATS)
|
368
|
+
|
369
|
+
response = requests.get(url, auth=auth)
|
370
|
+
|
371
|
+
data = response.json()['statuses']
|
372
|
+
|
373
|
+
# 2回目以降のリクエスト
|
374
|
+
|
375
|
+
cnt = 0
|
376
|
+
|
377
|
+
reply_cnt = 0
|
378
|
+
|
379
|
+
tweets = []
|
380
|
+
|
381
|
+
while True:
|
382
|
+
|
383
|
+
if len(data) == 0:
|
384
|
+
|
385
|
+
break
|
386
|
+
|
387
|
+
cnt += 1
|
388
|
+
|
389
|
+
if cnt > range:
|
390
|
+
|
391
|
+
break
|
392
|
+
|
393
|
+
for tweet in data:
|
394
|
+
|
395
|
+
if tweet['in_reply_to_status_id_str'] == tweet_id: # ツイートIDに一致するものを抽出
|
396
|
+
|
397
|
+
tweets.append([tweet['id_str']])#ツイートID
|
398
|
+
|
399
|
+
tweets.append(['{0:%Y-%m-%d %H:%M:%S}'.format(datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S %z %Y") + timedelta(hours=9))])# 投稿日
|
400
|
+
|
401
|
+
tweets.append([tweet['user']['name']])# ユーザー名
|
402
|
+
|
403
|
+
tweets.append([tweet['user']['id_str']])# ユーザーID
|
404
|
+
|
405
|
+
tweets.append([tweet['user']['screen_name']])# ユーザー表示名
|
406
|
+
|
407
|
+
tweets.append([tweet['text']]) # ツイート内容
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
reply_cnt += 1
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
maxid = int(tweet["id"]) - 1
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
url = "https://api.twitter.com/1.1/search/tweets.json?lang=ja&q="+user_id+"&count="+str(count)+"&max_id="+str(maxid)
|
420
|
+
|
421
|
+
response = requests.get(url, auth=auth)
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
try:
|
426
|
+
|
427
|
+
data = response.json()['statuses']
|
428
|
+
|
429
|
+
except KeyError: # リクエスト回数が上限に達した場合のデータのエラー処理
|
430
|
+
|
431
|
+
print('上限まで検索しました')
|
432
|
+
|
433
|
+
break
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
df = pd.DataFrame([tweets],columns=['TweetID',
|
438
|
+
|
439
|
+
'PostedTime',
|
440
|
+
|
441
|
+
'UserName',
|
442
|
+
|
443
|
+
'UserID',
|
444
|
+
|
445
|
+
'UserScreenName',
|
446
|
+
|
447
|
+
'PostMessage'])
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
df.to_csv('jr1.csv', encoding="utf-8") # CSV形式で保存
|
452
|
+
|
453
|
+
print('検索回数 :', cnt)
|
454
|
+
|
455
|
+
print('リプライ数 :', reply_cnt)
|
456
|
+
|
457
|
+
print(df)
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
return tweets
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
```
|
466
|
+
|
467
|
+
エラー文です。tweetsの中に横並びで324個のデータが入ってしまっているようです。
|
468
|
+
|
469
|
+
全文載せようとしましたが字数制限にひっかかってしまいました。
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
```エラー
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
ValueError: 6 columns passed, passed data had 324 columns
|
480
|
+
|
481
|
+
```
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
何度も申し訳ありません。分かる方がいらっしゃればよろしくお願いいたします。
|
2
エラー文の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,8 +16,140 @@
|
|
16
16
|
|
17
17
|
```
|
18
18
|
|
19
|
+
ValueError Traceback (most recent call last)
|
20
|
+
|
21
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)
|
22
|
+
|
23
|
+
1661 blocks = [
|
24
|
+
|
25
|
+
-> 1662 make_block(values=blocks[0], placement=slice(0, len(axes[0])))
|
26
|
+
|
27
|
+
1663 ]
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py in make_block(values, placement, klass, ndim, dtype)
|
32
|
+
|
33
|
+
2713
|
34
|
+
|
35
|
+
-> 2714 return klass(values, ndim=ndim, placement=placement)
|
36
|
+
|
37
|
+
2715
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim)
|
42
|
+
|
43
|
+
2369
|
44
|
+
|
45
|
+
-> 2370 super().__init__(values, ndim=ndim, placement=placement)
|
46
|
+
|
47
|
+
2371
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim)
|
52
|
+
|
53
|
+
129 raise ValueError(
|
54
|
+
|
55
|
+
--> 130 f"Wrong number of items passed {len(self.values)}, "
|
56
|
+
|
57
|
+
131 f"placement implies {len(self.mgr_locs)}"
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
ValueError: Wrong number of items passed 1, placement implies 6
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
During handling of the above exception, another exception occurred:
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
ValueError Traceback (most recent call last)
|
70
|
+
|
71
|
+
<ipython-input-78-0f418a9fed69> in <module>
|
72
|
+
|
73
|
+
1 if __name__ == '__main__':
|
74
|
+
|
75
|
+
----> 2 main()
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
<ipython-input-61-f197ee9a00ff> in main()
|
80
|
+
|
81
|
+
13 range = 100 # 検索回数の上限値(最大180/15分でリセット)
|
82
|
+
|
83
|
+
14 # ツイート検索・リプライの抽出
|
84
|
+
|
85
|
+
---> 15 tweets = search_tweets(CK, CKS, AT, ATS, user_id, tweet_id, count, range)
|
86
|
+
|
87
|
+
16
|
88
|
+
|
89
|
+
17
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
<ipython-input-77-a76659ff5e8f> in search_tweets(CK, CKS, AT, ATS, user_id, tweet_id, count, range)
|
94
|
+
|
95
|
+
36 'UserID',
|
96
|
+
|
97
|
+
37 'UserScreenName',
|
98
|
+
|
99
|
+
---> 38 'PostMessage'])
|
100
|
+
|
101
|
+
39
|
102
|
+
|
103
|
+
40 df.to_csv('jr1.csv', encoding="utf-8") # CSV形式で保存
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
|
108
|
+
|
109
|
+
521 mgr = arrays_to_mgr(arrays, columns, index, columns, dtype=dtype)
|
110
|
+
|
111
|
+
522 else:
|
112
|
+
|
113
|
+
--> 523 mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
|
114
|
+
|
115
|
+
524 else:
|
116
|
+
|
117
|
+
525 mgr = init_dict({}, index, columns, dtype=dtype)
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in init_ndarray(values, index, columns, dtype, copy)
|
122
|
+
|
123
|
+
232 block_values = [values]
|
124
|
+
|
125
|
+
233
|
126
|
+
|
127
|
+
--> 234 return create_block_manager_from_blocks(block_values, [columns, index])
|
128
|
+
|
129
|
+
235
|
130
|
+
|
131
|
+
236
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)
|
136
|
+
|
137
|
+
1670 blocks = [getattr(b, "values", b) for b in blocks]
|
138
|
+
|
139
|
+
1671 tot_items = sum(b.shape[0] for b in blocks)
|
140
|
+
|
141
|
+
-> 1672 raise construction_error(tot_items, blocks[0].shape[1:], axes, e)
|
142
|
+
|
143
|
+
1673
|
144
|
+
|
145
|
+
1674
|
146
|
+
|
147
|
+
|
148
|
+
|
19
149
|
ValueError: Shape of passed values is (6, 1), indices imply (6, 6)
|
20
150
|
|
151
|
+
|
152
|
+
|
21
153
|
```
|
22
154
|
|
23
155
|
csvも保存されておらずかつDataFrameも表示されません。
|
1
エラー文の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
```
|
18
18
|
|
19
|
-
Shape of passed values is (6, 1), indices imply (6, 6)
|
19
|
+
ValueError: Shape of passed values is (6, 1), indices imply (6, 6)
|
20
20
|
|
21
21
|
```
|
22
22
|
|