質問編集履歴

1

views.pyの中身を追加

2020/10/06 03:35

投稿

sakanaku
sakanaku

スコア17

test CHANGED
File without changes
test CHANGED
@@ -192,6 +192,124 @@
192
192
 
193
193
 
194
194
 
195
+ ### 該当のソースード
196
+
197
+ views.py
198
+
199
+ ```ここに言語名を入力
200
+
201
+ import chromedriver_binary
202
+
203
+ from selenium import webdriver
204
+
205
+ from selenium.webdriver.common.keys import Keys
206
+
207
+ from selenium.webdriver.chrome.options import Options
208
+
209
+ from .models import Meibo, Content
210
+
211
+ from django.urls import reverse_lazy
212
+
213
+ from django.views.generic import CreateView
214
+
215
+
216
+
217
+ options = Options()
218
+
219
+ options.headless = True
220
+
221
+ options.add_argument('--headless')
222
+
223
+ driver = webdriver.Chrome(options=options)
224
+
225
+ driver.get(
226
+
227
+ "動的なwebサイト")
228
+
229
+
230
+
231
+ class Create(CreateView):
232
+
233
+ template_name = 'index.html'
234
+
235
+ model = Meibo
236
+
237
+ fields = ('name', 'link', 'date')
238
+
239
+ success_url = reverse_lazy('lo')
240
+
241
+
242
+
243
+
244
+
245
+ def mou(request):
246
+
247
+ arry = []
248
+
249
+
250
+
251
+ selector = '日付の要素'
252
+
253
+ element = driver.find_elements_by_css_selector(selector)
254
+
255
+ element = [a.get_attribute("href") for a in element]
256
+
257
+ date_element = "リンク先の要素"
258
+
259
+ dates = driver.find_elements_by_css_selector(date_element)
260
+
261
+ dates = [b.text for b in dates]
262
+
263
+ # print(dates)
264
+
265
+
266
+
267
+ i = '名前の要素'
268
+
269
+ items = driver.find_elements_by_css_selector(i)
270
+
271
+ items = [c.text for c in items]
272
+
273
+
274
+
275
+ for x in items:
276
+
277
+ for y in element:
278
+
279
+ for z in dates:
280
+
281
+ arry.append([x, y, z])
282
+
283
+
284
+
285
+ table = Company.objects.create(name=items, link=element, date=dates)
286
+
287
+
288
+
289
+ for e in arry:
290
+
291
+ b = Meibo.objects.create(name=e[0], link=e[1], date=e[2])
292
+
293
+
294
+
295
+
296
+
297
+ param = {'content': table}
298
+
299
+
300
+
301
+ return render(request, 'index.html', param)
302
+
303
+
304
+
305
+
306
+
307
+ ```
308
+
309
+
310
+
311
+
312
+
195
313
  ### 補足情報(FW/ツールのバージョンなど)
196
314
 
197
315
  mac10.15.3