-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgov.py
More file actions
206 lines (181 loc) · 6.17 KB
/
gov.py
File metadata and controls
206 lines (181 loc) · 6.17 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
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
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
import csv
import time
buffer = []
def simple_get(url):
"""
Attempts to get the content at `url` by making an HTTP GET request.
If the content-type of response is some kind of HTML/XML, return the
text content, otherwise return None.
"""
try:
with closing(get(url, stream=True)) as resp:
if is_good_response(resp):
return resp.content
else:
return None
except RequestException as e:
log_error('Error during requests to {0} : {1}'.format(url, str(e)))
return None
def is_good_response(resp):
"""
Returns True if the response seems to be HTML, False otherwise.
"""
content_type = resp.headers['Content-Type'].lower()
return (resp.status_code == 200
and content_type is not None
and content_type.find('html') > -1)
def log_error(e):
"""
It is always a good idea to log errors.
This function just prints them, but you can
make it do anything.
"""
print(e)
def runPage(hyperlink):
raw_html = simple_get(hyperlink)
soup = BeautifulSoup(raw_html, 'html.parser')
topnum = soup.find('span', attrs={"style": "display:none"})
topnum = topnum.text.strip()
topnum = int(topnum[1:-1])
return topnum
def getIssueData(hyperlink):
soupy = BeautifulSoup(simple_get(hyperlink), 'html.parser')
title = soupy.find('h1')
title = title.text.strip().split()
titl = ''
for t in range(len(title)):
if title[t] == 'Congress':
titl = title[:t]
titl[t-1]= titl[t-1][:-5]
title = ''
for t in titl:
title = title + ' ' + t
title = title.strip()
if title.lower().find('impeachment') > 0:
return '', title
elif "treaty" in hyperlink:
return 'International Affars', title
#policy area
policy_area = soupy.findAll('div', attrs={'class': 'tertiary_section'})
poli = []
for p in policy_area:
poli.append(p.text.strip())
poli = poli[-1].split()
poli = poli[4:-2]
policy = ''
for p in poli:
policy = policy + ' ' + p
policy = policy.strip()
return policy, title
def runSingleVote(hyperlink):
#Setup
print(hyperlink)
t = 0
raw_html = 0
soup = 0
try:
raw_html = simple_get(hyperlink)
soup = BeautifulSoup(raw_html, 'html.parser')
#Question Name
question = soup.find('div', attrs={"style": "padding-bottom:10px;"})
question = question.text.strip()[10:].split()
ques = ''
for q in question:
ques = ques + ' ' + q
if hyperlink in buffer:
buffer.remove(hyperlink)
except:
print('no')
buffer.append(hyperlink)
return
#Measure Number
measure = soup.find('div', attrs={'class': 'contenttext', "style": "padding-bottom:10px;"})
meas = ''
if measure:
measure = measure.text.strip().split()[2:]
for m in measure:
meas = meas + ' ' + m
meas = meas.strip()
else:
meas = "Motion"
#Measure URL
urls = soup.findAll('a')
url = ''
for l in urls:
u =l.get('href')
if u != None and u[:4] == 'http' and not 'amendment' in u:
url = u
break
if url == '' or url == 'https://www.congress.gov/':
url = "Motion"
#votes
votes = soup.find('span', attrs={'class': 'contenttext'})
votes = votes.text.strip().split()
iter_votes = iter(votes)
#date
date = soup.findAll('div', attrs={"style": "float:left; min-width:200px; padding-bottom:10px;", 'class': 'contenttext'})
da = []
for d in date:
da.append(d.text.strip())
date = da[1][11:]
#category
resTitle = ''
policyArea = ''
category = ''
if meas[:2] == 'PN':
category = 'Presidential Nomination'
elif url == 'Motion':
category = 'Assorted Motion'
else:
policyArea, resTitle = getIssueData(url)
if resTitle.lower().find('impeachment') > 0:
category = 'Impeachment of the President'
else:
category = policyArea
#writing
with open('votes.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
#for each one write
for i in range(len(votes)//3):
try:
name = next(iter_votes)
party = next(iter_votes)
if party[0] != '(':
name= name + ' ' + party
party = next(iter_votes)
state = party[3:5]
party = party[1]
vote = next(iter_votes)
if name[0:7] == "Voting ":#@devpost viewers: STANFORD EECS PEAK PERFORMANCE
name = name[7:]
writer.writerow([name, party, state, ques, meas, url, date, vote, category])
except:
break
def runYear(year):
congress = (year - 1787)/2.0
session = 1
if congress != round(congress):
session = 2
congress = int(congress)
senate_link = 'https://www.senate.gov/legislative/LIS/roll_call_lists/vote_menu_' + str(congress) + '_' + str(session) + '.htm'
largest_num = runPage(senate_link)
with open('votes.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Name', 'Party', 'State', 'Question', 'Measure', 'URL', 'Date', 'Vote', 'Category'])
for i in range(largest_num):
num = str(i+1)
# time.sleep(2)
while len(num) < 5:
num = '0' + num
runSingleVote("https://www.senate.gov//legislative/LIS/roll_call_lists/roll_call_vote_cfm.cfm?congress=" + str(congress) + "&session=" + str(session) + "&vote=" + num)
while buffer:
time.sleep(120)
for hyperlink in buffer:
runSingleVote(hyperlink)
time.sleep(30)
if __name__== "__main__":
runYear(2019)