回答編集履歴

5

Fix code

2020/08/10 06:03

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -610,7 +610,7 @@
610
610
 
611
611
 
612
612
 
613
- list_response = ParallelHtmlScraper.execute(f'{host_google}', path_and_content, AnalyzerForTest())
613
+ list_response = ParallelHtmlScraper.execute(host_google, path_and_content, AnalyzerForTest())
614
614
 
615
615
  print(list_response)
616
616
 
@@ -624,9 +624,9 @@
624
624
 
625
625
  ```python
626
626
 
627
- $ python test.py
627
+ $ pipenv run python test.py
628
-
628
+
629
- ['Google', 'Google 画像検索', 'Google ショッピング', 'コレクション', 'Google マップ', '\n Google ドライブ\n ', '\n Gmail - Google のメール\n ']
629
+ ['Google 画像検索', ' Google マップ ', '\n Gmail - Google のメール\n ', 'Google ショッピング', 'Google', '\n Google ドライブ\n ', 'コレクション']
630
630
 
631
631
  ```
632
632
 

4

Fix code

2020/08/10 06:02

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -610,7 +610,7 @@
610
610
 
611
611
 
612
612
 
613
- list_response = ParallelHtmlScraper.execute(f'{host_google}', path_and_content.keys(), AnalyzerForTest())
613
+ list_response = ParallelHtmlScraper.execute(f'{host_google}', path_and_content, AnalyzerForTest())
614
614
 
615
615
  print(list_response)
616
616
 

3

Fix link to Stack Overflow

2020/08/08 03:09

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -98,7 +98,7 @@
98
98
 
99
99
 
100
100
 
101
- 参考: [Answer: How could I use requests in asyncio? ](https://stackoverflow.com/a/47572164/12721873)
101
+ 参考: [Answer: How could I use requests in asyncio? ](https://stackoverflow.com/a/22414756/12721873)
102
102
 
103
103
 
104
104
 

2

Fix answer that mistook intention of question

2020/07/01 21:46

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -62,6 +62,46 @@
62
62
 
63
63
 
64
64
 
65
+ ## スレッドから非同期への変更
66
+
67
+
68
+
69
+ 次の箇所を変更します:
70
+
71
+
72
+
73
+ ```python
74
+
75
+ with ThreadPoolExecutor(max_workers=2) as pool:
76
+
77
+ threads = [res for res in pool.map(check_url, urls)]
78
+
79
+ ```
80
+
81
+
82
+
83
+
84
+
85
+ ```python
86
+
87
+ async def asynchronous_process(urls):
88
+
89
+ loop = asyncio.get_event_loop()
90
+
91
+ futures = [loop.run_in_executor(None, check_url, url) for url in urls]
92
+
93
+ await asyncio.gather(*futures)
94
+
95
+ asyncio.run(asynchronous_process(urls))
96
+
97
+ ```
98
+
99
+
100
+
101
+ 参考: [Answer: How could I use requests in asyncio? ](https://stackoverflow.com/a/47572164/12721873)
102
+
103
+
104
+
65
105
  ## 検証
66
106
 
67
107
 
@@ -72,6 +112,8 @@
72
112
 
73
113
  ```python
74
114
 
115
+ import asyncio
116
+
75
117
  import requests
76
118
 
77
119
  import json
@@ -86,7 +128,7 @@
86
128
 
87
129
  from bs4 import BeautifulSoup
88
130
 
89
- from concurrent.futures import ThreadPoolExecutor
131
+ # from concurrent.futures import ThreadPoolExecutor
90
132
 
91
133
 
92
134
 
@@ -160,8 +202,6 @@
160
202
 
161
203
  def check_url(target_url, headers=headers, proxies=proxies, retry=3):
162
204
 
163
-
164
-
165
205
  for i in range(retry):
166
206
 
167
207
 
@@ -174,8 +214,6 @@
174
214
 
175
215
  #ここ↓のリクエストが時間がかかるので非同期処理をさせたい
176
216
 
177
-
178
-
179
217
  req = requests.get(
180
218
 
181
219
  target_url, headers=headers, proxies=proxies, allow_redirects=False
@@ -186,6 +224,8 @@
186
224
 
187
225
  target_urlll = re.sub("(.*)(?=/)|/|(?=?)(.*)", "", target_url)
188
226
 
227
+ print(target_url)
228
+
189
229
  print(str(req.status_code) + "\t" + target_urlll)
190
230
 
191
231
 
@@ -344,7 +384,7 @@
344
384
 
345
385
 
346
386
 
347
- # return
387
+ return
348
388
 
349
389
  except requests.exceptions.ConnectTimeout:
350
390
 
@@ -376,13 +416,23 @@
376
416
 
377
417
 
378
418
 
379
- threads = []
419
+ # threads = []
380
-
381
-
382
-
420
+
421
+
422
+
383
- with ThreadPoolExecutor(max_workers=2) as pool:
423
+ # with ThreadPoolExecutor(max_workers=()) as pool:
384
-
424
+
385
- threads = [res for res in pool.map(check_url, urls)]
425
+ # threads = [res for res in pool.map(check_url, urls)]
426
+
427
+ async def asynchronous_process(urls):
428
+
429
+ loop = asyncio.get_event_loop()
430
+
431
+ futures = [loop.run_in_executor(None, check_url, url) for url in urls]
432
+
433
+ await asyncio.gather(*futures)
434
+
435
+ asyncio.run(asynchronous_process(urls))
386
436
 
387
437
 
388
438
 
@@ -440,12 +490,16 @@
440
490
 
441
491
  ```console
442
492
 
443
- $ pipenv run python test2.py
493
+ $ pipenv run python test.py
444
494
 
445
495
  https://www.yahoo.co.jp/
446
496
 
447
497
  200
448
498
 
499
+ https://www.facebook.com/
500
+
501
+ 302
502
+
449
503
  https://www.google.com/
450
504
 
451
505
  200
@@ -454,10 +508,6 @@
454
508
 
455
509
  200
456
510
 
457
- https://www.facebook.com/
458
-
459
- 302
460
-
461
511
  https://www.instagram.com/
462
512
 
463
513
  200
@@ -472,7 +522,7 @@
472
522
 
473
523
 
474
524
 
475
- elapsed_time:1.1018426418304443[sec]
525
+ elapsed_time:0.526411771774292[sec]
476
526
 
477
527
 
478
528
 
@@ -482,7 +532,9 @@
482
532
 
483
533
 
484
534
 
535
+ レスポンスの早さの違いによって
536
+
485
- レスポンスの早さによって結果が入れ替わっているのがわかります
537
+ 実行結果の出力順 `list.txt` の順序と入れ替わっているのがわかります
486
538
 
487
539
 
488
540
 
@@ -490,7 +542,7 @@
490
542
 
491
543
 
492
544
 
493
- 以前にほぼ同様のことを実現するためのライブラリーを作成しました
545
+ 以前にほぼ同様のことを実現するためのパッケを作成しました
494
546
 
495
547
 
496
548
 
@@ -504,9 +556,11 @@
504
556
 
505
557
 
506
558
 
507
- ライブラリーとして使えば再発明の必要がなくなりますし、
559
+ パッケとして使えば再発明の必要がなくなりますし、
560
+
508
-
561
+ パッケージのコードの中身を読んでいただくと、
562
+
509
- コードの中身を読んでいただくと、どのように実装すべきかが理解いただけると思います
563
+ どのように実装すべきかが理解いただけると思います
510
564
 
511
565
 
512
566
 
@@ -544,11 +598,11 @@
544
598
 
545
599
  '/shopping?hl=ja&source=og&tab=wf', # Google ショッピング
546
600
 
547
- '/save', # Google Collection
601
+ '/save', # コレクション
548
-
602
+
549
- 'https://www.google.co.jp/maps', # Google Maps
603
+ 'https://www.google.co.jp/maps', # Google マップ
550
-
604
+
551
- 'https://www.google.co.jp/drive/apps.html', # Google Drive
605
+ 'https://www.google.co.jp/drive/apps.html', # Google ドライブ
552
606
 
553
607
  'https://www.google.co.jp/mail/help/intl/ja/about.html?vm=r', # GMail
554
608
 
@@ -575,3 +629,13 @@
575
629
  ['Google', 'Google 画像検索', 'Google ショッピング', 'コレクション', 'Google マップ', '\n Google ドライブ\n ', '\n Gmail - Google のメール\n ']
576
630
 
577
631
  ```
632
+
633
+
634
+
635
+ このパッケージを実装していたときは、
636
+
637
+ とにかく公式ドキュメントばかりを読んでいた記憶があります
638
+
639
+
640
+
641
+ [コルーチンと Task — Python 3.8.4rc1 ドキュメント](https://docs.python.org/ja/3.8/library/asyncio-task.html)

1

Update description

2020/07/01 21:40

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- 1.
9
+ 1
10
10
 
11
11
  `ThreadPoolExecutor()` の引数 `max_workers` に int 型の値を設定します:
12
12
 
@@ -34,7 +34,7 @@
34
34
 
35
35
 
36
36
 
37
- 2.
37
+ 2
38
38
 
39
39
  少なくとも、同じリソースへの繰り返しアクセスは 1 秒以上間隔を空けた方が良いでしょう:
40
40
 
@@ -506,7 +506,11 @@
506
506
 
507
507
  ライブラリーとして使えば再発明の必要がなくなりますし、
508
508
 
509
- コードを読んでいただくと、どのように実装すべきかが理解いただけると思います
509
+ コードの中身を読んでいただくと、どのように実装すべきかが理解いただけると思います
510
+
511
+
512
+
513
+ ライブラリーの利用例:
510
514
 
511
515
 
512
516