-
Notifications
You must be signed in to change notification settings - Fork 4
/
scrape.py
788 lines (654 loc) · 28.2 KB
/
scrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
import argparse
import datetime
import os
import shutil
import signal
import time
import concurrent.futures
from pathlib import Path
from dotenv import load_dotenv
from selenium.common.exceptions import (
NoSuchElementException,
TimeoutException,
StaleElementReferenceException,
WebDriverException,
ElementNotInteractableException,
ElementClickInterceptedException,
)
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait, Select
import urllib3
from captcha_solver import azcaptcha_solver_post
from constants import list_all_comb
from utils import (
download_wait,
is_element_present,
kill_os_process,
setup_selenium_browser_driver,
kill_web_drivers,
clear_temp_folder,
logger,
)
load_dotenv()
LINK = "https://cej.pj.gob.pe/cej/forms/busquedaform.html"
PLACEHOLDER_TEXT = "--SELECCIONAR"
DONE_FLAG = "NO MORE FILES"
default_temp_download_folder = os.path.join(
os.path.realpath(os.path.dirname(__file__)), "temp_downloads"
)
faulty_downloads_dir = os.path.join(
os.path.realpath(os.path.dirname(__file__)), "faulty_downloads"
)
final_data_folder = os.path.join(os.path.realpath(os.path.dirname(__file__)), "data")
if not os.path.exists(faulty_downloads_dir):
p = Path(faulty_downloads_dir)
p.mkdir(parents=True)
if not os.path.exists(default_temp_download_folder):
p = Path(default_temp_download_folder)
p.mkdir(parents=True)
if not os.path.exists(final_data_folder):
p = Path(final_data_folder)
p.mkdir(parents=True)
NUMBER_OF_WORKERS = int(os.getenv("NUMBER_OF_WORKERS", 5))
drivers = []
stop_threads = False
threads = []
global_executor = None
def mark_combo_file_num_done(combo, file_num, parent_dir):
done_dir = os.path.join(parent_dir, "done")
if not os.path.exists(done_dir):
Path(done_dir).mkdir(parents=True)
done_file_path = os.path.join(done_dir, get_done_filename(combo, file_num))
Path(done_file_path).touch()
def mark_combo_done(combo, parent_dir):
done_dir = os.path.join(parent_dir, "done")
if not os.path.exists(done_dir):
Path(done_dir).mkdir(parents=True)
done_file_path = os.path.join(done_dir, get_done_filename(combo))
Path(done_file_path).touch()
def is_combo_file_num_done(combo, file_num, parent_dir):
done_dir = os.path.join(parent_dir, "done")
done_file_path = os.path.join(done_dir, get_done_filename(combo, file_num))
return os.path.exists(done_file_path)
def validate_locations_choice(value):
choices = list(c[0] for c in list_all_comb)
if value in choices or value == "":
return value
raise argparse.ArgumentTypeError(
f"{value} is not a valid choice. Available choices: {choices}"
)
def parse_args():
parser = argparse.ArgumentParser(
description="Scrape course case data from https://cej.pj.gob.pe/cej/forms/busquedaform.html"
)
current_year = datetime.datetime.now().year
parser.add_argument(
"-y",
"--years",
dest="years",
action="store",
type=int,
nargs="*",
choices=list(range(2005, current_year + 1)),
default=None,
help="years to scrape, default to 2019",
required=True,
)
parser.add_argument(
"-l" "--locations",
dest="locations",
type=str,
nargs="+",
default=None,
help="locations to scrape, default to all",
)
args = parser.parse_args()
if args.locations:
parsed_location_list = [s.strip() for s in ",".join(args.locations).split(",")]
parsed_location_list = [
validate_locations_choice(s) for s in parsed_location_list if s
]
return parsed_location_list, args.years
return None, args.years
def get_latest_locations():
clear_temp_folder(default_temp_download_folder)
try:
logger.info("getting latest locations...")
driver = setup_selenium_browser_driver(default_temp_download_folder)
driver.get(LINK)
loc_dropdown = Select(driver.find_element(By.ID, "distritoJudicial"))
locations = set(option.text for option in loc_dropdown.options)
locations.remove(PLACEHOLDER_TEXT)
driver.quit()
return locations
except (
NoSuchElementException,
TimeoutException,
StaleElementReferenceException,
WebDriverException,
) as e:
logger.error(e)
os._exit(2)
def get_done_filename(combo, file_num=None):
arr = combo
if file_num is not None:
arr = combo + ["file_num", str(file_num)]
return "_".join(arr)
def get_year_done_filename(year):
return os.path.join(final_data_folder, str(year), "done")
def get_all_valid_years():
driver = setup_selenium_browser_driver(default_temp_download_folder)
driver.get(LINK)
element = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element((By.ID, "anio"), PLACEHOLDER_TEXT)
)
loc_dropdown = Select(driver.find_element(By.ID, "anio"))
years = set(option.text for option in loc_dropdown.options)
years.remove(PLACEHOLDER_TEXT)
driver.quit()
return sorted([int(y) for y in years], reverse=True)
def get_parent_raw_html_dir(year):
return os.path.join(final_data_folder, str(year), "raw_html")
def is_year_done(year):
return os.path.exists(get_year_done_filename(year))
def is_combo_done(combo, parent_dir):
done_file_path = os.path.join(parent_dir, "done", get_done_filename(combo))
return os.path.exists(done_file_path)
def mark_year_done(year):
Path(get_year_done_filename(year)).touch()
def enable_download_in_headless_chrome(driver, download_dir):
driver.command_executor._commands["send_command"] = (
"POST",
"/session/$sessionId/chromium/send_command",
)
params = {
"cmd": "Page.setDownloadBehavior",
"params": {"behavior": "allow", "downloadPath": download_dir},
}
driver.execute("send_command", params)
class Scrapper:
def __init__(self) -> None:
pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
def scrape_data(
self, driver, temp_downloads_dir
): # to scrape the insides of the site
time.sleep(2)
button_list = [] # To scrape the button type links of the documents
try:
button_list = driver.find_elements(
By.XPATH, '//div[@class="celdCentro"]/form/button'
)
except (
NoSuchElementException,
TimeoutException,
StaleElementReferenceException,
WebDriverException,
):
logger.error(
"Error occurred in getting button links, restarting scraping from the current file number"
)
driver.quit()
raise RuntimeError("Error Occurred")
table_html = []
case_names_list = []
if len(button_list) == 0:
no_files_flag = True
else:
no_files_flag = False
logger.info(f"button list: {len(button_list)}")
for index in range(len(button_list)):
logger.info(
"--" + str(index) + "--"
) # This will tell you which doc is being processed
# wait 10 seconds before looking for element
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, '//div[@class="celdCentro"]/form/button')
)
)
except (
NoSuchElementException,
TimeoutException,
StaleElementReferenceException,
WebDriverException,
):
driver.quit()
logger.warning(
"Error occurred in getting button links, restarting scraping from the current file number"
)
raise RuntimeError("Error Occurred")
button_list = []
button_list = driver.find_elements(
By.XPATH, '//div[@class="celdCentro"]/form/button'
)
try:
button_list[index].click()
except (ElementNotInteractableException, ElementClickInterceptedException):
driver.execute_script(
f'document.querySelectorAll("div.celdCentro form button")[{index}].click()'
)
logger.info(str(index) + " button clicked")
############################################################################################################
# Important part of the code, while scraping htmls, it might not load after clicking just once, this way of doing it if it does not load solves the issue
link_1 = None
attempts = 0
while not link_1:
try:
link_1 = driver.find_element(By.XPATH, '//div[@class="partes"]')
except NoSuchElementException:
button_list = []
button_list = driver.find_elements(
By.XPATH, '//div[@class="celdCentro"]/form/button'
)
if index < len(button_list):
button_list[index].click()
attempts += 1
if attempts >= 5:
return None, None, None
#############################################################################################################
html = driver.page_source
table_html.append(html)
# driver.execute_script("var scrollingElement = (document.scrollingElement ||
# document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
if is_element_present(
"xpath", '//div[@class="celdaGrid celdaGridXe"]', driver
):
tags = driver.find_elements(
By.XPATH, '//div[@class="celdaGrid celdaGridXe"]'
)
else:
logger.warning(
"Error occured, restarting scraping from the current file number"
)
raise RuntimeError("Error Occured")
for tag_index in range(len(tags)):
case_names_list.append(tags[tag_index].text)
# for downloading the documents
elements_doc = []
logger.info({"case_names_list:": case_names_list})
try:
if is_element_present(
"xpath", '//div[@class="panel panel-default divResolPar"]', driver
):
elements_doc = driver.find_elements(By.CLASS_NAME, "aDescarg")
expediente_n = driver.find_element(
By.CLASS_NAME, "celdaGrid.celdaGridXe"
).text
expediente_year = expediente_n.split("-")[1]
existing_faulty_files = os.listdir(faulty_downloads_dir)
expediente_downloads_file = str(expediente_n) + ".txt"
if expediente_downloads_file in existing_faulty_files:
continue
for i in range(len(elements_doc)):
subfolder = str(expediente_n) + "_" + str(i + 1)
attributeValue_link = elements_doc[i].get_attribute("href")
target_download_dir = os.path.join(
final_data_folder,
expediente_year,
"downloaded_files",
subfolder,
)
if not os.path.exists(target_download_dir):
p = Path(target_download_dir)
p.mkdir(parents=True)
# WebDriverWait(driver, 30).until(
# EC.element_to_be_clickable(elements_doc[i])
# ).click()
driver.get(attributeValue_link)
link_path = target_download_dir + "/link.txt"
with open(link_path, "w+") as f:
f.write(str(attributeValue_link))
f.close()
timeout_time = (
10 # wait at max 10 seconds for a file to download
)
download_wait(temp_downloads_dir, timeout_time, driver, False)
file_names = os.listdir(temp_downloads_dir)
if len(file_names) > 0:
while len(file_names) > 0:
temp_file_path = os.path.join(
temp_downloads_dir, file_names[0]
)
shutil.move(temp_file_path, target_download_dir)
logger.info(f"{file_names[0]} downloaded")
file_names = os.listdir(temp_downloads_dir)
else:
logger.info("file not downloaded, will retry")
success = self.retry_download(
elements_doc[i],
4,
target_download_dir,
temp_downloads_dir,
driver,
)
if not success:
faulty_downloads_path = os.path.join(
faulty_downloads_dir, f"{expediente_n}.txt"
)
Path(faulty_downloads_path).touch()
except (
TimeoutException,
StaleElementReferenceException,
WebDriverException,
) as e:
logger.warning(
f"Error occurred in getting links of download files, restarting scraping from the current file "
f"number:\n: {e.msg}"
)
raise RuntimeError("Error Occurred")
finally:
element_back = "https://cej.pj.gob.pe/cej/forms/resumenform.html"
driver.get(element_back)
return table_html, case_names_list, no_files_flag
# This function saves the extracted data in CSV format
def html_saver(self, case_names_list, path, table_html):
parent_dir = str(path) + "/"
for index in range(len(table_html)):
logger.info("--" + str(index) + "--")
file = str(case_names_list[index]) + ".txt"
with open(os.path.join(parent_dir, file), "w") as fp:
fp.write(table_html[index])
# For entering the site and scraping everything inside
# This is the master function
# Please do not tamper with the sleep timers anywhere in this code
def scraper(
self, file_num, list_comb, driver, year, temp_downloads_dir, attempts=0
):
try:
driver.get(LINK)
# driver.maximize_window()
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element(
(By.ID, "distritoJudicial"), PLACEHOLDER_TEXT
)
)
# selecting LIMA
select = Select(driver.find_element(By.ID, "distritoJudicial"))
select.select_by_visible_text(str(list_comb[0]))
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element((By.ID, "anio"), PLACEHOLDER_TEXT)
)
# selecting YEAR
select = Select(driver.find_element(By.ID, "anio"))
select.select_by_visible_text(str(year))
# For JUZGADO DE PAZ LETRADO
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element(
(By.ID, "organoJurisdiccional"), PLACEHOLDER_TEXT
)
)
# Selecting instance of the case as JUZGADO DE PAZ LETRADO
select = Select(driver.find_element(By.ID, "organoJurisdiccional"))
select.select_by_visible_text(str(list_comb[1]))
# Civil inside JUZGADO DE PAZ LETRADO
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element(
(By.ID, "especialidad"), PLACEHOLDER_TEXT
)
)
select = Select(driver.find_element(By.ID, "especialidad"))
select.select_by_visible_text(
str(list_comb[2])
) # Set to civil, can be changed to any other type depending on the requirements of the user
# input case file num
inputElement = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "numeroExpediente"))
)
inputElement.send_keys(file_num)
# driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
driver.execute_script(
"var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;"
)
no_more_element_is_displayed = False
sleep_time = 3
index = 0
while not is_element_present(
"xpath", '//div[@class="celdCentro"]/form/button', driver
):
if index != 0:
if is_element_present("id", "mensajeNoExisteExpedientes", driver):
no_more_element_is_displayed = driver.find_element(
By.ID, "mensajeNoExisteExpedientes"
).is_displayed()
if no_more_element_is_displayed:
break
else:
if is_element_present("id", "btnReload", driver):
logger.warning("Captcha solved incorrectly, retrying...")
if not inputElement.get_attribute("value"):
# restart scraper since input values are empty
return self.scraper(
file_num,
list_comb,
driver,
year,
temp_downloads_dir,
)
driver.find_element(By.ID, "btnReload").click()
time.sleep(3)
if is_element_present("id", "btnReload", driver):
captcha_text = azcaptcha_solver_post(driver)
captcha = driver.find_element(By.ID, "codigoCaptcha")
captcha.clear()
captcha.send_keys(captcha_text)
driver.find_element(
By.XPATH, '//*[@id="consultarExpedientes"]'
).click()
while True:
try:
loader_is_displayed = driver.find_element(
By.ID, "cargando"
).is_displayed()
if not loader_is_displayed:
break
except Exception:
break
time.sleep(sleep_time)
index += 1
logger.info("Captcha solved correctly")
if not no_more_element_is_displayed:
parent_dir = get_parent_raw_html_dir(year)
directory = "_".join(list_comb + ["file_num", str(file_num)])
path = os.path.join(parent_dir, directory)
if not os.path.exists(path):
Path(path).mkdir(parents=True)
try:
logger.info(f"processing file_num: {file_num} for {list_comb}")
table_html, case_names_list, no_files = self.scrape_data(
driver, temp_downloads_dir
)
except RuntimeError:
return self.scraper(
file_num, list_comb, driver, year, temp_downloads_dir
)
if table_html is None and case_names_list is None:
logger.warning(
f"Failed to click form button, restarting file_num {file_num}"
)
if attempts < 5:
return self.scraper(
file_num,
list_comb,
driver,
year,
temp_downloads_dir,
attempts + 1,
)
else:
raise RuntimeError("Error Occurred")
if no_files:
logger.info("NO MORE FILES, DELAYED ERROR")
flag = "NO MORE FILES, DELAYED ERROR"
return flag
else:
self.html_saver(case_names_list, path, table_html)
mark_combo_file_num_done(list_comb, file_num, parent_dir)
combo_flag = "Combo Done"
return combo_flag
else:
logger.info(f"NO MORE FILES for {list_comb}")
return DONE_FLAG
except Exception as e:
if isinstance(e, KeyboardInterrupt):
handle_keyboard_cancel(None, None)
elif isinstance(e, PermissionError):
logger.warning(
"restarting scraping from the current file number due to PermissionError"
)
return self.scraper(
file_num, list_comb, driver, year, temp_downloads_dir
)
elif isinstance(e, urllib3.connectionpool.MaxRetryError):
logger.warning(f"Max retries exceeded: {e.max_retries}")
else:
failed_file = f"{year}-{'-'.join(list_comb)}-{file_num}"
faulty_downloads_path = os.path.join(
faulty_downloads_dir, f"{failed_file}.txt"
)
logger.warning(f"adding failed_file {failed_file} to faulty downloads")
Path(faulty_downloads_path).touch()
def retry_download(
self, link_el, max_tries, target_download_dir, temp_downloads_dir, driver
):
tries = 1
timeout_time = 10 # wait at max 10 seconds for a file to download
success = False
while tries < max_tries and not success:
link_el.click()
download_wait(temp_downloads_dir, timeout_time, driver, False)
file_names = os.listdir(temp_downloads_dir)
if len(file_names) > 0:
temp_file_path = os.path.join(temp_downloads_dir, file_names[0])
shutil.move(temp_file_path, target_download_dir)
logger.info("downloaded on try : " + str(tries))
success = True
else:
logger.info(
"file not downloaded on try "
+ str(tries)
+ " ,"
+ str(max_tries - tries)
+ " left"
)
tries = tries + 1
return success
def scrape_for_each_comb(self, list_comb, year, parent_raw_html_directory):
if not stop_threads:
logger.info(f"Start processing {year} {list_comb}")
else:
os._exit(1)
file_number = (
1 # reset case number to start from 1 for each location-court-type combo
)
flag = ""
empty_num = 0
temp_downloads_dir = os.path.join(
default_temp_download_folder, "_".join(list_comb)
)
if not os.path.exists(temp_downloads_dir):
p = Path(temp_downloads_dir)
p.mkdir(parents=True)
web_driver = setup_selenium_browser_driver(temp_downloads_dir)
drivers.append(web_driver)
enable_download_in_headless_chrome(web_driver, temp_downloads_dir)
while flag != DONE_FLAG and empty_num < 5 and not stop_threads:
if is_combo_file_num_done(
list_comb, file_number, parent_raw_html_directory
):
logger.info(f"Already done. Skipping {list_comb} {file_number}")
file_number = file_number + 1
continue
flag = self.scraper(
file_number, list_comb, web_driver, year, temp_downloads_dir
)
logger.info(f"{list_comb} file no {file_number}'s flag: {flag}")
if flag == "NO MORE FILES, DELAYED ERROR" or not flag:
empty_num = empty_num + 1
logger.info(
"File was empty, if next "
+ str(5 - empty_num)
+ " files are empty, next combination will start"
)
file_number = file_number + 1
web_driver.quit()
if not os.listdir(temp_downloads_dir): # delete temp folder
os.rmdir(temp_downloads_dir)
mark_combo_done(list_comb, parent_raw_html_directory)
return f"Done processing {year} {list_comb}"
def handle_keyboard_cancel(signal, frame):
logger.warning("Stopping threads...")
# Handle the keyboard interrupt signal by setting the global flag
global stop_threads
stop_threads = True
os._exit(1)
if __name__ == "__main__":
kill_os_process("chrome")
kill_os_process("chromedriver")
# Register the keyboard interrupt signal handler
signal.signal(signal.SIGINT, handle_keyboard_cancel)
locations, years = parse_args()
valid_locations = get_latest_locations()
logger.info(
f"All valid locations according to the current location dropdown menu: {valid_locations}"
)
if not years:
years = get_all_valid_years()
for scrape_year in years:
if is_year_done(scrape_year):
logger.info(f"Skipping {scrape_year} as it is already done")
continue
# try:
locations_to_use = list_all_comb
if locations and len(locations) > 0:
locations_to_use = [
x for x in list_all_comb if x[0] in locations
] # only use parsed locations
max_workers = (
NUMBER_OF_WORKERS
if NUMBER_OF_WORKERS <= len(locations_to_use)
else NUMBER_OF_WORKERS - (NUMBER_OF_WORKERS - len(locations_to_use))
)
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
for location_list in locations_to_use:
parent_raw_html_dir = get_parent_raw_html_dir(scrape_year)
if is_combo_done(location_list, parent_raw_html_dir):
logger.info(
f"Skipping {scrape_year} {location_list} as it is already done"
)
continue
if locations and location_list[0] not in locations:
continue
if location_list[0] not in valid_locations:
logger.warning(
f"Skipping {location_list} as {location_list[0]} is not found in the current location dropdown menu"
)
continue
scrapper_thread = Scrapper()
threads.append(
executor.submit(
scrapper_thread.scrape_for_each_comb,
location_list,
scrape_year,
parent_raw_html_dir,
)
)
try:
while not stop_threads:
for thread in concurrent.futures.as_completed(threads):
thread.result()
except KeyboardInterrupt:
executor.shutdown(wait=False)
handle_keyboard_cancel(None, None)
if not locations and not stop_threads:
mark_year_done(scrape_year)
kill_web_drivers(drivers)