-
Notifications
You must be signed in to change notification settings - Fork 21
/
bgbl_scraper.py
executable file
·172 lines (149 loc) · 5.68 KB
/
bgbl_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
#!/usr/bin/env python3
"""BGBl-Scraper.
Usage:
bgbl_scaper.py <outputfile> [update | <minyear> [<maxyear>]]
bgbl_scaper.py -h | --help
bgbl_scaper.py --version
Options:
-h --help Show this screen.
--version Show version.
Examples:
bgbl_scaper.py data/bgbl.json
"""
import sys
from pathlib import Path
import urllib.parse
import re
import json
from collections import defaultdict
import time
import roman_numbers
import lxml.html
from lxml.cssselect import CSSSelector
import requests
from typing import List
class BGBLScraper:
BASE_URL = 'http://www.bgbl.de/xaver/bgbl/'
def __init__(self):
self.session = requests.session()
self.session.get(self.BASE_URL + 'start.xav') # save cookies
def downloadUrl(self, file, query={}):
query['request.preventCache'] = int(time.time()*1000)
response = self.session.get(f'{self.BASE_URL}{file}?{urllib.parse.urlencode(query)}')
return response.json()
def downloadToc(self, toc_id = 0):
response = self.downloadUrl('ajax.xav', {'q': 'toclevel', 'n': str(toc_id)})
return response['items'][0]
def downloadText(self, toc_id, doc_id) -> lxml.html.HtmlElement:
query = {
'tf': 'xaver.component.Text_0',
'hlf': 'xaver.component.Hitlist_0',
'tocid': str(toc_id),
'start': f"//*[@node_id='{doc_id}']",
}
response = self.downloadUrl('text.xav', query)
return lxml.html.fromstring(response['innerhtml'])
def scrape(self, year_low=0, year_high=sys.maxsize):
self.year_low = year_low
self.year_high = year_high
collection = {}
for part, part_data in self.get_toc().items():
for year, year_data in part_data.items():
for number, number_data in year_data.items():
items = []
for item in number_data:
item['part'] = part
item['year'] = year
item['number'] = number
items.append(item)
collection[f'{part}_{year}_{number}'] = items
return collection
def get_toc(self):
response = self.downloadToc()
assert response['l'] == "Bundesgesetzblatt"
result = {}
for item in response['c']:
match = re.match(r'Bundesgesetzblatt Teil ([IVXLCDM]+)', item['l'], re.IGNORECASE)
if match:
part = roman_numbers.number(match.group(1))
print(f"Getting Part TOC {part}")
result[part] = self.get_part_toc(item['id'])
return result
def get_part_toc(self, part_id):
response = self.downloadToc(part_id)
assert response['id'] == part_id
result = {}
for item in response['c']:
year = int(item['l'])
if not (self.year_low <= year <= self.year_high):
continue
print(f"Getting Year TOC {year}")
result[year] = self.get_year_toc(item['id'])
return result
def get_year_toc(self, year_id):
response = self.downloadToc(year_id)
assert response['id'] == year_id
result = {}
for item in response['c']:
match = re.match(r'Nr\. (\d+) vom (\d{2}\.\d{2}\.\d{4})', item['l'])
if match:
number = int(match.group(1))
date = match.group(2)
print(f"Getting Number TOC {number} from {date}")
result[number] = self.get_number_toc(item['id'], item['did'])
return result
def get_number_toc(self, number_id, number_did):
#response = self.downloadToc(number_id)
root = self.downloadText(number_id, number_did)
toc = []
for tr in root.cssselect('tr'):
td: lxml.html.HtmlElement = tr.cssselect('td')[1]
divs: List[CSSSelector] = td.cssselect('div')
law_date = None
if not len(divs):
continue
if len(divs) == 2:
divs = [None] + divs
else:
law_date = divs[0].text_content().strip()
link = divs[1].cssselect('a')[0]
name = link.text_content().strip()
href = link.attrib['href']
text = divs[2].text_content().strip()
match = re.search(r'aus +Nr. +(\d+) +vom +(\d{1,2}\.\d{1,2}\.\d{4}),'
r' +Seite *(\d*)\w?\.?$', text)
page = None
date = match.group(2)
if match.group(3):
page = int(match.group(3))
kind = 'entry'
if name in ('Komplette Ausgabe', 'Inhaltsverzeichnis'):
# FIXME: there are sometimes more meta rows
kind = 'meta'
d = {
'kind': kind,
'date': date,
'law_date': law_date,
'name': name,
'page': page,
'href': self.BASE_URL + href,
}
toc.append(d)
return toc
def main(arguments):
bgbl = BGBLScraper()
data = {}
if Path(arguments['<outputfile>']).exists():
with open(arguments['<outputfile>']) as f:
data = json.load(f)
minyear = int(arguments['<minyear>'] or 0)
maxyear = int(arguments['<maxyear>'] or sys.maxsize)
if arguments['update'] and len(data) > 0:
minyear = max([toc_entry['year'] for pub in data.values() for toc_entry in pub])
data.update(bgbl.scrape(minyear, maxyear))
with open(arguments['<outputfile>'], 'w') as f:
json.dump(data, f, indent=4)
if __name__ == '__main__':
from docopt import docopt
arguments = docopt(__doc__, version='BGBl-Scraper 0.0.1')
main(arguments)