Repository navigation
Expand file tree
/
Copy pathget_AZ.py
More file actions
77 lines (68 loc) · 2.73 KB
/
Copy pathget_AZ.py
File metadata and controls
77 lines (68 loc) · 2.73 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
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
import configparser
from utils import Crawler as CoreCrawler
import re
from datetime import datetime
import os
import sys
class Crawler(CoreCrawler):
abbr = 'AZ'
def _get_remote_filename(self, local_filename):
entity_name, year = local_filename[:-4].split('@#')
if re.search(r'County', entity_name):
directory = 'General Purpose'
else:
directory = 'Community College Districts'
filename = '{} {} {}.pdf'.format(self.abbr, entity_name, year)
return directory, filename, year
if __name__ == '__main__':
script_name = 'get_AZ.py'
start_time = datetime.utcnow()
result = 1
error_message = ""
config_file = ""
config = configparser.ConfigParser()
config.read('conf.ini')
file_name = ''
crawler = Crawler(config, 'arizona', script_name, error_message)
error_message = crawler.error_message
try:
if error_message != "":
raise Exception(error_message)
config_file = str(crawler.dbparams)
downloads_path = crawler.get_property('downloads_path', 'arizona')
if not os.path.exists(downloads_path):
os.makedirs(downloads_path)
elif not os.path.isdir(downloads_path):
print('ERROR: downloads_path parameter points to file!')
sys.exit(1)
crawler.get(config.get('arizona', 'url'))
td_e = crawler.get_elements('td')
for x in range(5, len(td_e)):
td_e = crawler.get_elements('td')
a = td_e[x].find_element_by_tag_name('a')
file_name = crawler.get_text('a', root=td_e[x])
year = crawler.get_text('span', root=td_e[x])[5:]
a.click()
files = crawler.get_elements('span.file')
url_a = files[0].find_element_by_tag_name('a')
type = url_a.text
url = url_a.get_attribute('href')
if type == 'Highlights':
pass
else:
downloaded = crawler.download(url, '{}@#{}.pdf'.format(file_name[:re.search('June',
file_name).start()].replace(
' ', ''), year))
if downloaded:
crawler.upload_to_file_storage(
'{}@#{}.pdf'.format(file_name[:re.search('June',
file_name).start()].replace(' ', ''), year))
crawler.back()
except Exception as e:
result = 0
error_message = str(e)
print(error_message)
finally:
end_time = datetime.utcnow()
crawler.db.log(script_name, start_time, end_time, config_file, error_message)
crawler.close()