-
Notifications
You must be signed in to change notification settings - Fork 0
/
company_info.py
73 lines (51 loc) · 1.97 KB
/
company_info.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
import argparse
import csv
import time
import urllib.request
from selenium import webdriver
import paths.advanced_search as s
import paths.company_info as i
import utils.captcha as c
def get_arguments():
"""Gets arguments from the command line.
Returns:
A parser with the input arguments.
"""
parser = argparse.ArgumentParser(usage='Loads a companies .csv and extracts information over JUCESP.')
parser.add_argument('input_file', help='Input .csv file', type=str)
return parser.parse_args()
if __name__ == '__main__':
# Gathers the input arguments
args = get_arguments()
# Gathering variables from arguments
input_file = args.input_file
captcha_file = 'captcha.png'
# Creates the webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(s.WAITING_TIME)
# Gets the search URL and uses a caveat to only input captcha once
driver.get(s.URL)
driver.find_element_by_xpath(s.FORM_SUBMIT).click()
img = driver.find_element_by_xpath(s.CAPTCHA)
urllib.request.urlretrieve(img.get_attribute('src'), captcha_file)
solved_captcha = c.solve(captcha_file)
driver.find_element_by_xpath(s.CAPTCHA_INPUT).send_keys(solved_captcha)
driver.find_element_by_xpath(s.CAPTCHA_SUBMIT).click()
# Loads the input .csv file
with open(input_file) as f:
# Creates an .csv reader
reader = csv.reader(f, delimiter=';')
# Iterates over the reader
for row in reader:
print(f'Dumping data from ID: {row[0]} ...')
# Gets the URL
driver.get(i.URL + row[0])
# Sleeps for a short amount of time
time.sleep(i.WAITING_TIME)
# Gathers the results
results = driver.find_element_by_xpath(i.RESULTS).get_attribute('outerHTML')
# Opens an output file
with open(f'companies/{row[0]}.html', 'w') as f2:
f2.write(results)
print('Data dumped.')
driver.close()