blob: 146119e8c673a5ade3d2ef1088cdab8cb46a8d7a (
plain)
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
|
import csv
from time import sleep
from tinydb import TinyDB
from modules.scraper_orders import ScraperOrders
from selenium.webdriver.common.by import By
from concurrent.futures import ThreadPoolExecutor, as_completed
import threading
class ThreadSafeDB:
def __init__(self):
self.db = TinyDB('orders.json')
self.lock = threading.Lock()
def insert(self, record):
with self.lock:
self.db.insert(record)
print(f'INSERTED: {record}')
db = ThreadSafeDB()
def scrape_single_court(row):
try:
config = {}
scraper = ScraperOrders(db, config)
scraper.close_modal()
scraper.select('sess_state_code', row[0])
scraper.select('sess_dist_code', row[1])
while True:
sleep(0.5)
try:
modal_is_open = scraper.driver.find_element(By.CLASS_NAME, 'modal').is_displayed()
if modal_is_open:
scraper.close_modal()
continue
break
except:
break
scraper.select('court_complex_code', row[2])
sleep(1)
scraper.goto_courtnumber()
sleep(1)
scraper.select('nnjudgecode1', row[3])
sleep(1)
scraper.driver.find_element(By.ID, 'radBoth2').click()
scraper.submit_search()
scraper.parse_orders_table()
scraper.handle_orders(row[3])
scraper.driver.quit()
except Exception as e:
print(f"Error processing court {row}: {e}")
def scrape_orders(courts_csv):
with open(courts_csv, newline='') as csvfile:
reader = csv.reader(csvfile)
courts = list(reader)
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [
executor.submit(scrape_single_court, court)
for court in courts
]
for future in as_completed(futures):
try:
future.result()
except Exception as e:
print(f"A thread encountered an error: {e}")
if __name__ == '__main__':
input_file = 'csv/2023-24_pocso.csv'
scrape_orders(input_file)
|