-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathscript_parallel.py
More file actions
35 lines (30 loc) · 1.02 KB
/
script_parallel.py
File metadata and controls
35 lines (30 loc) · 1.02 KB
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
import datetime
from itertools import repeat
from time import sleep, time
from multiprocessing import Pool, cpu_count
from scrapers.scraper import get_driver, connect_to_base, \
parse_html, write_to_file
def run_process(page_number, filename):
browser = get_driver()
if connect_to_base(browser, page_number):
sleep(2)
html = browser.page_source
output_list = parse_html(html)
write_to_file(output_list, filename)
browser.quit()
else:
print('Error connecting to hackernews')
browser.quit()
if __name__ == '__main__':
# set variables
start_time = time()
output_timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
output_filename = f'output_{output_timestamp}.csv'
# scrape and crawl
with Pool(cpu_count()-1) as p:
p.starmap(run_process, zip(range(1, 21), repeat(output_filename)))
p.close()
p.join()
end_time = time()
elapsed_time = end_time - start_time
print(f'Elapsed run time: {elapsed_time} seconds')