/
scrape.py
executable file
·394 lines (318 loc) · 12.3 KB
/
scrape.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/python
# vim: set fileencoding=utf-8
"""Voting Records scraper.
Scrape the voting records of the Georgian Parliament.
"""
__docformat__ = "epytext en"
import sys, os, getopt, urllib2, re, json, time
from BeautifulSoup import BeautifulSoup
HOST = 'http://www.parliament.ge/'
DOC = 'index.php'
PARAMS = '?sec_id=1530&lang_id=GEO&kan_name=&kan_num=&kan_mp=&Search=ძიება'
ROOT = HOST + DOC + PARAMS
NEXT_PAGE = ROOT + '&limit='
#HOST = 'file:///home/shensche/votingrecord/Parliament.ge-VotingRecord-Scraper/'
#ROOT = HOST + 'first.html'
SLEEP = 1 #: how long to wait between request
JSON_INDENT = 2 #: indentation for JSON output
OUTDIR = os.getcwd() + os.sep #: default output directory
class ScrapeError (Exception):
"""Generic scrape exception."""
def __init__(self, value):
super(ScrapeError, self).__init__()
self.value = value
def __str__(self):
return repr(self.value)
class VotingRecordsScraper (object):
"""Class to scrape the voting records."""
def __init__ (self, outdir):
"""Constructor
@param outdir: output directory
@type outdir: string
"""
if outdir[-1] != os.sep:
self.outdir = outdir + os.sep
else:
self.outdir = outdir
def _find_not_in_hidethis (self, tags):
"""
Filter tags which are not inside a parent tag with class 'hidethis'.
@param tags: tags to filter
@type tags: [ BeautifulSoup.Tag ]
@return: filtered tags
@rtype: [ BeautifulSoup.Tag ]
"""
data = []
for tag in tags:
hidethis = False
for attr in tag.parent.parent.parent.parent.attrs:
if attr[0] == 'class' and attr[1] == 'hidethis':
hidethis = True
for attr in tag.parent.parent.parent.attrs:
if attr[0] == 'class' and attr[1] == 'hidethis':
hidethis = True
if not hidethis:
data.append(tag)
return data
def _get_kan_id (self, tag):
"""
Get kan_id in given 'a'-tag.
kan_id seems to be the only identifier for the voting records data.
@param tag: tag representing an HTML <a>
@type tag: BeautifulSoup.Tag
@return: the kan_id
@rtype: string
"""
return tag.attrs[0][1].split('=')[-1]
def _get_next_page (self, soup, is_root=False):
"""
Determine the URL of the next page to scrape.
@param soup: soup to look for next page indicator
@type soup: BeautifulSoup.BeautifulSoup
@param is_root: if current page is the root page
@type is_root: bool
@return: URL of next page to scrape
@rtype: string
"""
regex = re.compile('limit=')
tags = soup.findAll('a', attrs={'href':regex})
if len(tags) < 5 and not is_root: # last page
return None
limit = ''
for attr in tags[-2].attrs: # -2 is next page
if attr[0] == 'href':
limit = attr[1].split('=')[-1]
return NEXT_PAGE + limit.encode('utf-8')
def _write (self, details, numbers, dates, results, amendments):
"""
Write voting records to (JSON) files.
@param details: scraped bill details
@type details: [ {'uri': string, 'name': string, 'kan_id': string} ]
@param numbers: scraped bill numbers
@type numbers: [ int ]
@param dates: scraped bill dates
@type dates: [ int ]
@param results: scraped bill voting results
@type results: [ [ {'name' : string, 'vote': string} ] ]
@param amendments: scraped amendment bill numbers
@type amendments: [ [ string ] ]
"""
for i in xrange(len(details)):
record = {
'kan_id': details[i]['kan_id'],
'url': HOST + details[i]['uri'],
'name': details[i]['name'],
'number': numbers[i],
'date': dates[i],
'result': results[i],
'amendments': amendments[i]
}
fmt = 'Voting record for bill %s, kan_id %s'
print fmt % (record['number'], record['kan_id'])
fname = self.outdir + record['kan_id'] + '.json'
handle = open(fname, 'w')
json.dump(record, handle, indent=JSON_INDENT)
handle.close()
def _scrape_details (self, soup):
"""
Find all bill details in given soup.
A detail contains info about uri to the actual bill, name and kan_id.
@param soup: soup to scrape
@type soup: BeautifulSoup.BeautifulSoup
@return: scraped bill details: relative uri, name, kan_id or None
@rtype: [ {'uri': string, 'name': string, 'kan_id': string} ]
"""
regex = re.compile('kan_det=det')
tags = soup.findAll('a', attrs={'href':regex})
tags = self._find_not_in_hidethis(tags)
data = []
for anchor in tags:
data.append({
'uri': anchor.attrs[0][1],
'name': anchor.contents[0].string.strip(),
'kan_id': self._get_kan_id(anchor)
})
if len(data) == 0:
print 'WARNING: No bills found. Is this the right document?'
return None
return data
def _scrape_numbers (self, soup, num_details):
"""
Find all bill numbers in given soup.
@param soup: soup to scrape
@type soup: BeautifulSoup.BeautifulSoup
@param num_details: number of bill details
@type num_details: int
@return: scraped bill numbers
@rtype: [ int ]
"""
regex = re.compile('^\s?\d\S*') # can't mach [-–], so using \S ??
tags = soup.findAll('td', attrs={'width':'50', 'align':'center'})
tags = self._find_not_in_hidethis(tags)
data = []
for tag in tags:
if len(tag.contents) == 0: # no bill number
data.append(None)
else:
if re.match(regex, tag.contents[0].string):
data.append(tag.contents[0].string.strip())
num = len(data)
if num != num_details:
fmt = 'Number mismatch: bill numbers %d != bill details %d'
raise ScrapeError(fmt % (num, num_details))
return data
def _scrape_dates (self, soup, num_details):
"""
Find all bill dates in given soup.
@param soup: soup to scrape
@type soup: BeautifulSoup.BeautifulSoup
@param num_details: number of bill details
@type num_details: int
@return: scraped bill dates
@rtype: [ int ]
"""
regex = re.compile('^\d{4}-\d{2}-\d{2}$')
tags = soup.findAll('td', attrs={'width':80, 'align':'center'})
tags = self._find_not_in_hidethis(tags)
data = []
for tag in tags:
if re.match(regex, tag.contents[0].string):
data.append(tag.contents[0].string)
num = len(data)
if num != num_details:
fmt = 'Number mismatch: bill dates %d != bill details %d'
raise ScrapeError(fmt % (num, num_details))
return data
def _scrape_results (self, soup, num_details):
"""
Find all bill voting results in given soup.
@param soup: soup to scrape
@type soup: BeautifulSoup.BeautifulSoup
@param num_details: number of bill details
@type num_details: int
@return: scraped bill voting results (vote + name)
@rtype: [ [ {'name' : string, 'vote': string} ] ]
"""
regex = re.compile('kan_det=res')
tags = soup.findAll('a', attrs={'href':regex})
data = []
for anchor in tags:
handle = urllib2.urlopen(HOST + anchor.attrs[0][1])
#handle = urllib2.urlopen(HOST + 'res.html')
result = BeautifulSoup(
handle, convertEntities=BeautifulSoup.HTML_ENTITIES)
table = result.find('table', attrs={
'width': '500',
'border':'0',
'align':'left',
'cellpadding':'3',
'cellspacing':'2',
'bgcolor':'#EEEEEE'
})
votes = []
for tag in table.findAll('tr', attrs={'bgcolor':'#FFFFFF'}):
children = tag.findChildren()
votes.append({
'name': children[0].contents[0].string,
'vote': children[1].contents[0].string
})
handle.close()
data.append(votes)
time.sleep(SLEEP) # give the server some time to breathe
num = len(data)
if num != num_details:
for _ in xrange(abs(num_details - num)): # fill with empty values
data.append(None)
return data
def _scrape_amendments (self, soup, details):
"""
Find all amendment bill numbers in given soup.
@param soup: soup to scrape
@type soup: BeautifulSoup.BeautifulSoup
@param details: scraped bill details
@type details: [ {'uri': string, 'name': string, 'kan_id': string} ]
@return scraped amendment bill numbers
@rtype: [ [ string ] ]
"""
# prepopulate data to be returned
data = [[] for _ in xrange(len(details))]
# each div contains a list of amendments
tags = soup.findAll('div', attrs={'class':'hidethis'})
for div in tags:
# find parent bill's index for this div's kan_id
bill = div.parent.parent.findPreviousSibling()
kan_id = self._get_kan_id(bill.find('td').find('a'))
for det in details:
if det['kan_id'] == kan_id:
idx = details.index(det)
break
# find amendment numbers
nums = []
for row in div.findAll('tr'):
cols = row.findAll('td')
if len(cols[1].contents) == 0:
nums.append(None)
else: # use bill number
nums.append(cols[1].contents[0])
data[idx] = nums
return data
def scrape (self, url, is_root=False):
"""
Scrape one page and return the next page's URL.
@param url: URL of the page to scrape
@type url: string
@param is_root: if given url is root page (used for getting next page)
@type is_root: bool
@return: URL of the next page to scrape
@rtype: string
"""
time_start = time.time()
print 'Scraping: %s' % url
handle = urllib2.urlopen(url)
soup = BeautifulSoup(
handle, convertEntities=BeautifulSoup.HTML_ENTITIES)
details = self._scrape_details(soup)
if details:
num_details = len(details)
numbers = self._scrape_numbers(soup, num_details)
dates = self._scrape_dates(soup, num_details)
results = self._scrape_results(soup, num_details)
amendments = self._scrape_amendments(soup, details)
self._write(details, numbers, dates, results, amendments)
handle.close()
nxt = self._get_next_page(soup, is_root)
print 'Time: Page %d seconds.' % (time.time() - time_start)
return nxt
def run (self):
"""Run the complete scraping process."""
print 'Output directory: %s' % self.outdir
time_start = time.time()
nxt = self.scrape(ROOT, is_root=True)
while nxt:
nxt = self.scrape(nxt, is_root=False)
time.sleep(SLEEP) # give the server some time to breathe
print 'Time: Overall %d seconds.' % (time.time() - time_start)
def main():
"""Main function in case the class isn't used directly."""
# parse command line options
try:
opts, _ = getopt.getopt(sys.argv[1:], 'ho:', ['help', 'output='])
except getopt.error, msg:
print msg
print 'For help use --help'
sys.exit(2)
# process options
outdir = None
for opt, arg in opts:
if opt in ("-h", "--help"):
print __doc__
sys.exit(0)
elif opt in ("-o", "--output"):
outdir = arg
if not outdir:
outdir = OUTDIR
if not os.path.exists(outdir):
os.mkdir(outdir)
VotingRecordsScraper(outdir).run()
if __name__ == "__main__":
main()