-
Notifications
You must be signed in to change notification settings - Fork 0
/
aid-scraper.py
334 lines (273 loc) · 9.31 KB
/
aid-scraper.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
#!/bin/env python2
# -*- coding: utf-8 -*-
"""
Fetches, parses and stores the data from the austrian aid projects website since January 1st 2010.
"""
import urllib2
import bs4
from bs4 import BeautifulSoup
from datetime import datetime
import re
import json
import os
import string
import time
__author__ = "Christian Goebel, Stefan Kasberger"
__copyright__ = "Copyright 2015"
__license__ = "MIT"
__version__ = "0.3"
__maintainer__ = "Stefan Kasberger"
__email__ = "mail@stefankasberger.at"
__status__ = "Production" # 'Development', 'Production' or 'Prototype'
### GLOBAL ###
ROOT_FOLDER = os.path.dirname(os.getcwd())
FOLDER_RAW_HTML = ROOT_FOLDER + '/data/raw/html/'
FOLDER_CSV = ROOT_FOLDER + '/data/csv/'
FOLDER_JSON = ROOT_FOLDER + '/data/json/'
FILENAME_BASE = 'aid-data'
BASE_URL = 'http://www.entwicklung.at/nc/zahlen-daten-und-fakten/projektliste/'
QUERY_URL = BASE_URL + '?tx_sysfirecdlist_pi1[test]=test&tx_sysfirecdlist_pi1[mode]=1&tx_sysfirecdlist_pi1[sort]=uid%3A1&tx_sysfirecdlist_pi1[pointer]='
DELAY_TIME = 2 # in seconds
# TS = datetime.now().strftime('%Y-%m-%d-%H-%M')
TS = '2016-04-03-03-05'
### FUNCTIONS ###
def SetupEnvironment():
"""Sets up the folder structure and working environment.
"""
if not os.path.exists(FOLDER_RAW_HTML):
os.makedirs(FOLDER_RAW_HTML)
if not os.path.exists(FOLDER_JSON):
os.makedirs(FOLDER_JSON)
if not os.path.exists(FOLDER_CSV):
os.makedirs(FOLDER_CSV)
def FetchHtml(url):
"""Fetches html url via urllib2.
Args:
url: url to fetch (string).
Returns:
html: html string as unicode.
"""
response = urllib2.urlopen(url)
html = response.read().decode('utf-8')
time.sleep(DELAY_TIME)
return html
def FetchHtmlTables(url, folder, pageCounter = 0):
"""Fetches each overview page with the table of the projects and saves the html locally.
Args:
url: string to fetch.
folder: directory to save the html files in.
pageCounter: counter for the actual number of the page (int).
"""
# the url has a page-counter at the end, so just increase the counter and fetch one page after another
rawHtml = FetchHtml(url+str(pageCounter))
if not os.path.exists(folder+TS):
os.makedirs(folder+TS)
Save2File(rawHtml, folder+TS+'/'+TS+'_table-'+str(pageCounter)+'.html')
# checks if there is a "weiter" (more) anchor, cause this means a next page exists
isWeiter = re.findall(r'<a href="(.*)" >weiter ></a></div></div>', rawHtml)
if isWeiter:
pageCounter += 1
FetchHtmlTables(url, folder, pageCounter)
def FetchHtmlProjects(aidData, folder):
"""Fetches html of the project pages and saves the html locally.
Args:
aidData: list[] of dict{} of aid projects.
folder: directory to save the html files in.
Returns:
aidData: list[] of dict{} of aid projects.
"""
if not os.path.exists(folder+TS):
os.makedirs(folder+TS)
# iterate over every list element to fetch and store the project page
for idx, project in enumerate(aidData):
html = FetchHtml(project['url'])
Save2File(html, folder+TS+'/'+TS+'_project-'+str(idx)+'.html')
return aidData
def Save2File(data, filename):
"""Saves a file locally.
Args:
data: string to save.
filename: name of the file with path.
"""
text_file = open(filename, "w")
text_file.write(data.encode('utf-8'))
text_file.close()
def ReadFile(filename):
"""Reads a file and returns the string.
Args:
filename: name of the file
Returns:
string: file as text-string.
"""
f = open(filename, 'r')
string = f.read()
return string
def ReadTableFilesInFolder(folder):
"""Reads in all table html-files from a folder.
Args:
folder: folder where the html-files are stored in.
Returns:
sortedList: list[] of sorted html texts.
"""
htmlList = []
sortedList = []
for filename in os.listdir(folder):
if filename.find(TS+'_table') >= 0:
rawHtml = ReadFile(folder+'/'+filename)
fileIndex = filename.split('.')[0].split('-')[5]
htmlList.append((int(fileIndex), rawHtml))
# sort list of duppels after first element (the filename postfix) and store to list[]
htmlList = sorted(htmlList, key=lambda htmlList: htmlList[0])
for idx, html in htmlList:
sortedList.append(html)
return sortedList
def ReadProjectFilesInFolder(aidData, folder):
"""Reads in all table html-files from a folder.
Args:
aidData: list[] of dict{} of aid projects.
folder: folder where the html-files are stored in.
Returns:
sortedList: list[] of sorted html texts.
"""
htmlList = []
sortedList = []
for filename in os.listdir(folder):
if filename.find(TS+'_project') >= 0:
rawHtml = ReadFile(folder+'/'+filename)
fileIndex = filename.split('.')[0].split('-')[5]
htmlList.append((int(fileIndex), rawHtml))
# sort list of duppels after first element (the filename postfix) and store to list[]
htmlList = sorted(htmlList, key=lambda htmlList: htmlList[0])
for idx, html in htmlList:
sortedList.append(html)
return sortedList
def ParseTables(html):
"""Parses each row of the html table.
Args:
html: list[] of duppels (filename, html).
Returns:
aidData: list[] of dict{} of aid projects.
'contract-number': contract number.
'contract-title': title of the project.
'country-region': country and/or region, where the project takes place.
'OEZA-ADA-contract-volume': amount of funding.
'contract-partner': partner organisation(s).
'url': url of the project page.
"""
aidData = []
counter = 1
for page in html:
soup = BeautifulSoup(page)
table = soup.find_all('table')[1]
for row in table.find_all('tr')[1:]:
project = {}
tds = row.find_all('td')
project['unique-id'] = str(counter)
project['contract-title'] = tds[1].div.text
project['url'] =tds[1].div.a['href']
project['country-region'] = tds[2].div.text
project['OEZA-ADA-contract-volume'] = tds[3].div.text
project['contract-partner'] = tds[4].div.text
project['contract-number'] = tds[0].div.text
aidData.append(project)
counter += 1
return aidData
def ParseProjects(aidData, htmlProjects):
"""Parses the description of every project page.
Args:
aidData: list[] of dict{} of aid projects.
htmlProjects: list[] of html texts.
Returns:
aidData: list[] of dict{} of aid projects.
'unique-id': unique id for each project.
'contract-number': contract number.
'contract-title': title of the project.
'country-region': country and/or region, where the project takes place.
'OEZA-ADA-contract-volume': amount of funding.
'contract-partner': partner organisation(s).
'description': description text of the project.
'url': url of the project page.
"""
for idx, html in enumerate(htmlProjects):
soup = BeautifulSoup(html)
aidData[int(idx)]['description'] = soup.find_all('table')[0].find_all('tr')[5].find_all('div')[1].text
return aidData
def SaveAidData(aidData, filename):
"""Saves the aid data as JSON file.
Args:
aidData: list[] of dict{} of aid projects.
filename: filepath of the JSON file.
"""
Save2File(json.dumps(aidData, indent=2, ensure_ascii=False), filename)
print 'Aid data exported as JSON:',filename
def OpenAidData(filename):
"""Opens the aid data JSON file.
Args:
filename: filepath of the JSON file.
"""
aidData = json.loads(ReadFile(filename))
return aidData
def Save2CSV(data, filename):
"""Exports the aid data into a structured csv file.
Args:
data: list[] of dict{} of aid projects.
filename: filepath.
"""
csvString = '"unique-id","contract-number","contract-title","OEZA-ADA-contract-volume","contract-partner","country-region","description","url"\n'
# iterate over each project
for project in data:
uniqueId = '""'
number = '""'
title = '""'
funding = '""'
partner = '""'
region = '""'
description = '""'
url = '""'
# read out each attribute
for elem in project.keys():
val = project[elem].replace('"', '').replace('\n', '').replace('\r', '') # replace apostrophes.
if elem == 'unique-id':
uniqueId = '"'+val+'"'
if elem == 'contract-number':
number = '"'+val+'"'
if elem == 'contract-title':
title = '"'+val+'"'
if elem == 'OEZA-ADA-contract-volume':
funding = '"'+val+'"'
if elem == 'contract-partner':
partner = '"'+val+'"'
if elem == 'country-region':
region = '"'+val+'"'
if elem == 'description':
description = '"'+val.strip('\n\r')+'"'
if elem == 'url':
url = '"'+val+'",'
csvString += uniqueId+','+number+','+title+','+funding+','+partner+','+region+','+description+','+url+'\n'
Save2File(csvString, filename)
print 'Aid data exported as CSV:',filename
if __name__ == "__main__":
# setup
startTime = datetime.now()
print 'start:', startTime
aidData = []
SetupEnvironment()
DOWNLOAD_FILES = False
PARSE_FILES = False
EXPORT_DATA = False
if DOWNLOAD_FILES:
FetchHtmlTables(QUERY_URL, FOLDER_RAW_HTML)
htmlTables = ReadTableFilesInFolder(FOLDER_RAW_HTML+TS)
aidData = ParseTables(htmlTables)
SaveAidData(aidData, FOLDER_JSON+FILENAME_BASE+'_'+TS+'.json')
FetchHtmlProjects(aidData, FOLDER_RAW_HTML)
if PARSE_FILES:
htmlTables = ReadTableFilesInFolder(FOLDER_RAW_HTML+TS)
aidData = ParseTables(htmlTables)
htmlProjects = ReadProjectFilesInFolder(aidData, FOLDER_RAW_HTML+TS)
aidData = ParseProjects(aidData, htmlProjects)
SaveAidData(aidData, FOLDER_JSON+FILENAME_BASE+'_'+TS+'.json')
if EXPORT_DATA:
aidData = OpenAidData(FOLDER_JSON+FILENAME_BASE+'_'+TS+'.json')
Save2CSV(aidData, FOLDER_CSV+FILENAME_BASE+'_'+TS+'.csv')
print 'runtime:', (datetime.now() - startTime)