回答編集履歴

6

変数名修正

2020/05/09 18:33

投稿

kotori_a
kotori_a

スコア724

test CHANGED
@@ -110,9 +110,9 @@
110
110
 
111
111
 
112
112
 
113
- def add_related_terms(self, related_terms):
113
+ def add_related_terms(self, related_term):
114
-
114
+
115
- self.related_terms.append(related_terms)
115
+ self.related_terms.append(related_term)
116
116
 
117
117
 
118
118
 
@@ -128,6 +128,10 @@
128
128
 
129
129
  soup1 = bs4.BeautifulSoup(driver.page_source, 'html.parser')
130
130
 
131
+
132
+
133
+ # Keywordオブジェクトを格納するためのリスト
134
+
131
135
  keywords = []
132
136
 
133
137
 

5

修正

2020/05/09 18:32

投稿

kotori_a
kotori_a

スコア724

test CHANGED
@@ -66,7 +66,7 @@
66
66
 
67
67
  #! python3.8
68
68
 
69
- # The modified parts of below codes, copyright: © 2020 taizan-hokuto
69
+ # copyright of below codes of modified part is © 2020 taizan-hokuto
70
70
 
71
71
  # Do not abuse.
72
72
 
@@ -188,41 +188,13 @@
188
188
 
189
189
  # --- ここからスクレイピング結果を出力するコード ---
190
190
 
191
-
192
-
193
- # 既に用意しているファイルを開く
194
-
195
- wb = openpyxl.Workbook()
196
-
197
-
198
-
199
- # # 既に用意したファイルがなければ新規ワークブックを作成
200
-
201
- # wb = openpyxl.Workbook()
202
-
203
-
204
-
205
- sheet = wb.active
206
-
207
- sheet.title = 'A_rank' # ついでに「シート名」を変更
208
-
209
-
210
-
211
- # フォントの設定
212
-
213
-
214
-
215
- # セルの設定
191
+ [略]
216
192
 
217
193
 
218
194
 
219
195
  # 「タイトル行」を入力(事前に入力していない場合)
220
196
 
221
- sheet["A1"].value = 'title'
197
+ []
222
-
223
- sheet["B1"].value = 'explaination'
224
-
225
- sheet["C1"].value = 'related_term'
226
198
 
227
199
 
228
200
 
@@ -232,11 +204,11 @@
232
204
 
233
205
  for keyword in keywords:
234
206
 
235
- sheet.cell(column = 1, row = cursor, value = keyword.title)
207
+ sheet.cell(column=1, row=cursor, value=keyword.title)
236
-
208
+
237
- sheet.cell(column = 2, row = cursor, value = ' '.join(keyword.explanations))
209
+ sheet.cell(column=2, row=cursor, value=' '.join(keyword.explanations))
238
-
210
+
239
- sheet.cell(column = 3, row = cursor, value = ','.join(keyword.related_terms))
211
+ sheet.cell(column=3, row=cursor, value=','.join(keyword.related_terms))
240
212
 
241
213
  cursor += 1
242
214
 
@@ -244,8 +216,6 @@
244
216
 
245
217
  # 保存して閉じる
246
218
 
247
- wb.save('./keyword_ver0.94.xlsx')
248
-
249
- wb.close()
219
+ [略]
250
-
220
+
251
- ```
221
+ ```

4

修正

2020/05/09 16:33

投稿

kotori_a
kotori_a

スコア724

test CHANGED
@@ -32,11 +32,11 @@
32
32
 
33
33
  class Keyword:
34
34
 
35
- title # キーワードテキスト
35
+ title # キーワードテキスト
36
-
36
+
37
- explanations = [] #説明 のリスト
37
+ explanations = [] # 説明 のリスト
38
-
38
+
39
- related_terms = [] #関連用語 のリスト
39
+ related_terms = [] # 関連用語 のリスト
40
40
 
41
41
  ```
42
42
 
@@ -58,7 +58,7 @@
58
58
 
59
59
 
60
60
 
61
- [一部略してあるところは変えていせんので、ご自分で補完してください]
61
+ [一部略してあので、ご自分で補完してください]
62
62
 
63
63
 
64
64
 
@@ -134,7 +134,7 @@
134
134
 
135
135
  for keyword_part in soup1.select('p a'):
136
136
 
137
- #キーワード文字列を取得
137
+ # キーワード文字列を取得
138
138
 
139
139
  title = keyword_part.getText()
140
140
 
@@ -142,48 +142,50 @@
142
142
 
143
143
  continue
144
144
 
145
- #Keyword オブジェクトの生成
145
+ # Keyword オブジェクトの生成
146
146
 
147
147
  kw = Keyword(title)
148
148
 
149
+
150
+
151
+ # キーワードの説明のページに遷移しHTMLを取得
152
+
153
+ url = keyword_part.get('href')
154
+
155
+ driver.get(url)
156
+
157
+ sleep(10)
158
+
159
+ soup2 = bs4.BeautifulSoup(driver.page_source, 'html.parser')
160
+
161
+ soup3 = bs4.BeautifulSoup(driver.page_source, 'html.parser')
162
+
163
+ for explanation_p in soup2.select('#body p'):
164
+
165
+ explanation = explanation_p.getText()
166
+
167
+ if ('歴史' in explanation) or ('【' in explanation):
168
+
169
+ continue
170
+
171
+ kw.add_explanation(explanation)
172
+
173
+ for related_term_a in soup3.select('#body a'):
174
+
175
+ related_term = related_term_a.getText()
176
+
177
+ if ('歴史' in related_term) or ('地理' in related_term) or ('公民' in related_term) or ('†' in related_term):
178
+
179
+ continue
180
+
181
+ kw.add_related_terms(related_term)
182
+
183
+
184
+
149
185
  keywords.append(kw)
150
186
 
151
187
 
152
188
 
153
- #キーワードの説明のページに遷移しHTMLを取得
154
-
155
- url = keyword_part.get('href')
156
-
157
- driver.get(url)
158
-
159
- sleep(10)
160
-
161
- soup2 = bs4.BeautifulSoup(driver.page_source, 'html.parser')
162
-
163
- soup3 = bs4.BeautifulSoup(driver.page_source, 'html.parser')
164
-
165
- for explanation_p in soup2.select('#body p'):
166
-
167
- explanation = explanation_p.getText()
168
-
169
- if ('歴史' in explanation) or ('【' in explanation):
170
-
171
- continue
172
-
173
- kw.add_explanation(explanation)
174
-
175
- for related_term_a in soup3.select('#body a'):
176
-
177
- related_term = related_term_a.getText()
178
-
179
- if ('歴史' in related_term) or ('地理' in related_term) or ('公民' in related_term) or ('†' in related_term):
180
-
181
- continue
182
-
183
- kw.add_related_terms(related_term)
184
-
185
-
186
-
187
189
  # --- ここからスクレイピング結果を出力するコード ---
188
190
 
189
191
 
@@ -196,7 +198,7 @@
196
198
 
197
199
  # # 既に用意したファイルがなければ新規ワークブックを作成
198
200
 
199
- #wb = openpyxl.Workbook()
201
+ # wb = openpyxl.Workbook()
200
202
 
201
203
 
202
204
 
@@ -228,8 +230,6 @@
228
230
 
229
231
  cursor = 2
230
232
 
231
-
232
-
233
233
  for keyword in keywords:
234
234
 
235
235
  sheet.cell(column = 1, row = cursor, value = keyword.title)
@@ -242,16 +242,10 @@
242
242
 
243
243
 
244
244
 
245
-
246
-
247
245
  # 保存して閉じる
248
246
 
249
247
  wb.save('./keyword_ver0.94.xlsx')
250
248
 
251
249
  wb.close()
252
250
 
253
-
254
-
255
-
256
-
257
- ```
251
+ ```

3

書き出しの要件を誤解していたため、修正しました。

2020/05/09 16:26

投稿

kotori_a
kotori_a

スコア724

test CHANGED
@@ -230,37 +230,17 @@
230
230
 
231
231
 
232
232
 
233
- for keyword_part in keywords:
233
+ for keyword in keywords:
234
-
235
- start_row = cursor
234
+
236
-
237
- sheet.cell(column = 1, row = cursor , value = keyword_part.title)
235
+ sheet.cell(column = 1, row = cursor, value = keyword.title)
238
-
239
- # explanationの書き込み
236
+
240
-
241
- for explanation in keyword_part.explanations:
242
-
243
- sheet.cell(column = 2, row = cursor, value = explanation)
237
+ sheet.cell(column = 2, row = cursor, value = ' '.join(keyword.explanations))
238
+
244
-
239
+ sheet.cell(column = 3, row = cursor, value = ','.join(keyword.related_terms))
240
+
245
- cursor += 1
241
+ cursor += 1
246
-
247
- end_of_explanation = cursor
242
+
248
-
249
- # 書き込み行をリセットする。
243
+
250
-
251
- cursor = start_row
252
-
253
- # related_termの書き込み
254
-
255
- for related_term in keyword_part.related_terms:
256
-
257
- sheet.cell(column = 3, row = cursor, value = related_term)
258
-
259
- cursor += 1
260
-
261
- end_of_related_term = cursor
262
-
263
- cursor = max(end_of_explanation, end_of_related_term)
264
244
 
265
245
 
266
246
 

2

不要行修正

2020/05/09 16:19

投稿

kotori_a
kotori_a

スコア724

test CHANGED
@@ -138,8 +138,6 @@
138
138
 
139
139
  title = keyword_part.getText()
140
140
 
141
- #キーワードのリンクのURLを取得
142
-
143
141
  if title == '歴史キーワード':
144
142
 
145
143
  continue

1

インデント

2020/05/09 16:04

投稿

kotori_a
kotori_a

スコア724

test CHANGED
@@ -1,6 +1,6 @@
1
1
  **[改善案]**
2
2
 
3
- 同一ページにある内容は、classを利用して、ひとまとまりのデータに集めてしまいましょう。
3
+ 同一ページにある内容は、クラスを利用して、ひとまとまりのデータに集めてしまいましょう。
4
4
 
5
5
 
6
6
 
@@ -16,7 +16,7 @@
16
16
 
17
17
  | キーワード
18
18
 
19
- | └キーワード文字列(タイトル)
19
+ |   └キーワード文字列(タイトル)
20
20
 
21
21
  |   └説明1、説明2、説明3・・・
22
22