-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
230 lines (202 loc) · 7.24 KB
/
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
import urlparse
import requests
import os
import bs4
import json
# Names of the files that are saved by the scraper
dipjson4='diputados_4.json'
votejson4='votations_1282-4982.json'
dipjson5='diputados_5.json'
votejson5='votations_5004-10960.json'
dipjson6='diputados_6.json'
votejson6='votations_11108-19012.json'
dipjson8 = 'diputados_8.json'
votejson8 = 'votations_19192-20702.json'
# Base URL's for the scraping
urlLegActual = 'http://opendata.congreso.cl/wscamaradiputados.asmx/getLegislaturaActual'
urlDip = 'http://opendata.congreso.cl/wscamaradiputados.asmx/getDiputados_Periodo?prmPeriodoID='
urlPeriodosLeg = 'http://opendata.congreso.cl/wscamaradiputados.asmx/getPeriodosLegislativos'
urlDipVig = 'http://opendata.congreso.cl/wscamaradiputados.asmx/getDiputados_Vigentes'
urlVotings = 'http://opendata.congreso.cl/wscamaradiputados.asmx/getVotacion_Detalle?prmVotacionID='
party_keys=["IND","DC","PPD","PRSD","PS","RN","UDI","PRI","PC","IC"]
#Range of Voting ID numbers for each period
votID20022006 = [1282, 4982]
votID20062010 = [5004, 10960]
votID20102014 = [11108, 19012]
votID20142018 = [19192, 20702]
######### Code Taken from PA1 in CS122 - UNIVERSITY OF CHICAGO #########
def get_request(url):
'''
Open a connection to the specified URL and if successful
read the data.
Inputs:
url: must be an absolute URL
Outputs:
request object or None
Examples:
get_request("http://www.cs.uchicago.edu")
'''
try:
html = requests.get(url)
return html
except:
# fail on any kind of error
return None
def read_request(request):
'''
Return data from request object. Returns result or "" if the read
fails.
'''
try:
return request.text.encode('utf-8') # We change the encoding
except:
print "read failed: " + request.url
return ""
####### END OF COPIED CODE ########
def read(url):
'''
Given a URL, it generates a request object, and then turns it into a
String, which is turned into a Beautiful Soup XML object (not html),
with utf-8 encoding.
Returns the Beautiful Soup object
'''
req = get_request(url)
xmlstring = read_request(req)
xmlsoup = bs4.BeautifulSoup(xmlstring, 'xml', from_encoding="utf-8")
return xmlsoup
def read_diputados(diputados_soup, id_legislatura):
'''
Takes a Beautiful Soup XML object of the hole page, and scrapes and
finds all the Representatives of a given period of legislature.
Legislature are named:
4 -> 2002-2006
5 -> 2006-2010
6 -> 2010-2014
8 -> 2014-2018 (current)
Returns a dictionary with all the representatives for a given period
'''
diputados = {}
dips_xml = diputados_soup.findAll('Diputado')
for diputado in dips_xml:
militancias = diputado.Militancias_Periodos.findAll('Militancia')
for periodo in militancias:
#print "I am in 2"
if periodo.Periodo.ID.text==id_legislatura:
#print 'I am in'
agregar_diputado(diputados, diputado, periodo)
return diputados
def agregar_diputado(diputados, diputado, periodo):
'''
It takes the dictionary created on read_diputados, a tag object with
the representative's information and another tag object with the party
Returns the updated dictionary.
'''
datos = [diputado.Nombre.get_text(), diputado.Apellido_Paterno.text,
diputado.Apellido_Materno.text, periodo.Periodo.Nombre.text,
periodo.Periodo.ID.text]
registro = {int(diputado.DIPID.text): datos}
partido = periodo.Partido['Codigo']
if partido in diputados.keys():
diputados[partido].update(registro)
else:
diputados[partido] = registro
def create_dipdict(num):
'''
Creates the Representatives dictionary by just specifing the
Legislatures ID number, calling the other functions.
Returns the Dictionary.
'''
num = (str)(num)
url = urlDip+num
if num==8:
url = urlDipVig
xml_string = read(url)
d = read_diputados(xml_string, num)
return d
def create_dip_json(num):
'''
Creates a JSON file with the representatives information, by specifing
the Legislatures ID.
The file is encoded in utf-8. Acknowledgments to Gustav Larsson
'''
n = str(num)
filename = 'diputados_'+n+'.json'
dips = create_dipdict(num)
open (filename, 'w').write(json.dumps(dips, ensure_ascii=False).encode('utf8'))
def read_votacion (votacion_soup, id_votacion):
'''
Given a tag object and a voting id, it returns a dictionary with
the details of how every representative voting in it.
'''
voting = {}
# This check if its and emty votation XML files. Without the checking
# the program would crash
if votacion_soup.ID:
if int(votacion_soup.ID.text)!=id_votacion: #Double checking
print "Error. Voting ID and XML Vote ID don't match"
print id_votacion
else:
votos = votacion_soup.findAll('Voto')
for diputado in votos:
voting[int(diputado.DIPID.text)] = int(diputado.Opcion['Codigo'])
return voting
def generate_voting_records(low, high):
'''
Given a higher and lower bound range of voting ID's, it scrapes all
the XML web pages on the server for each ID in between, generating a
dictionary with all the valid information.
It outputs percentage of completion on the console, because it can take
hours to run, depending on the speed of the web connection.
'''
voting_record = {}
mult = 100.0/(high-low) #the multiplier
for x in range(low, high+1): # must include upper bound
num = (str)(x)
url = urlVotings+num
votacion_soup = read(url)
record = read_votacion(votacion_soup, x)
# If its not an empty dictionary. Many voting ID's in between
# have no information.
if record:
voting_record[x] = record
completion = (x-low)*mult
print "{0:.2f}".format(completion)+'%' #Just show 2 decimals
return voting_record
def create_voting_json(low, high):
'''
Given the upper and lower bound, it creates a JSON file with the
dictionary of generating_voting_records in it.
'''
n = str(low)+'-'+str(high)
filename = 'votations_'+n+'.json'
votations = generate_voting_records(low, high)
with open (filename, 'w') as f:
json.dump(votations, f)
def votings(id_legis):
'''
It calls create_voting_json with the correct upper and lower bounds
by just specifing the Legislature ID
'''
n = str(id_legis)
ids = {'4': votID20022006, '5': votID20062010,
'6': votID20102014, '8': votID20142018}
if n in ids.keys():
create_voting_json(ids[n][0], ids[n][1])
else:
print "Error. Bad Period. 4, 5, 6 or 8 are valid"
def do():
'''
Runs the program, scrapes the web, creates the dictionaries
and creates 8 json files.
'''
periods = [4,5,6,8]
for x in periods:
print 'Create the representatives json file for period '+str(x)+'...'
create_dip_json(x)
print 'DONE'
print 'Create the votings json file for period '+str(x)+'...'
votings(x)
print 'DONE'
print 'Program FINISHED'
if __name__ == "__main__":
do()