-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapeBillTrack50.py
185 lines (155 loc) · 6.09 KB
/
ScrapeBillTrack50.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
#!/bin/env python3
"""ScrapeBillTrack50
This module allows the user to pull provide a list of legislator names and pull selected information from
the Bill Track 50 website (https://www.billtrack50.com).
Functions
---------
scrape_bill_track_50
The main function of the module
"""
from argparse import RawTextHelpFormatter
import sys
import urllib
from magiconfig import ArgumentParser, MagiConfigOptions
from googlesearch import search
from bs4 import BeautifulSoup
class Staffer:
"""A class which contains the information about a single Staffer.
Attributes
----------
name : str
The name of the staffer
title : str
The staffers official title
role_description : str
A description of the staffers role
location : str
The description of the office at which the staffer works
address : str
The address to the office at which the staffer works
phone : str
The phone number of the staffer
email : str
The email address of the staffer
"""
def __init__(self, name, title, role_description, location, address, phone, email):
"""This method initializes the data members of the Staffer class.
Parameters
----------
name : str
The name of the staffer
title : str
The staffers official title
role_description : str
A description of the staffers role
location : str
The description of the office at which the staffer works
address : str
The address to the office at which the staffer works
phone : str
The phone number of the staffer
email : str
The email address of the staffer
"""
self.name = name
self.title = title
self.role_description = role_description
self.location = location
self.address = address
self.phone = phone
self.email = email
def __repr__(self):
"""Return a formated string representation of the Staffer object"""
formatted_fields = ', '.join(f"{key} = {value}" for key, value in self.__dict__.items())
return f"Staffer({formatted_fields})"
def __str__(self):
"""Return a formatted string to print for the Staffer object"""
return f"Staffer({self.name} | {self.title} | {self.role_description} | {self.email})"
def get_schedulers_from_table(staff_list, quiet = False):
"""From a HTML table, get the list of schedulers
Parameters
----------
staff_list : list
A list of HTML table rows
quiet : bool
If Ture, silences the printouts
"""
schedulers = []
for staff in staff_list:
fields = staff.find_all('td')
is_scheduler = any("Scheduler" in field for field in fields)
if is_scheduler:
schedulers.append(
Staffer(name = fields[0].string,
title = fields[1].string,
role_description = fields[2].string,
location = fields[3].string,
address = fields[4].string,
phone = fields[5].string,
email = fields[6].string)
)
if not quiet:
print(f"\t\t{schedulers[-1]}")
return schedulers
def scrape_bill_track_50(argv = None):
"""The main function for this scrip
Parameters
----------
argv : str, optional
A list of command line arguments
"""
if argv is None:
argv = sys.argv[1:]
parser = ArgumentParser(config_options=MagiConfigOptions(),
formatter_class=RawTextHelpFormatter,
description="Scrape https://www.BillTrack50.com for specific information.",
epilog = """examples:
using a magiconfig file:
`python3 ScrapeBillTrack50.py -C configs/assignments.py`"""
)
parser.add_argument("-d", "--debug", action = "store_true",
help = "Shows some extra information in order to debug this program (default=%(default)s)")
parser.add_argument("-l", "--legislators", nargs = "+",
help = "A list of names of legislators (default=%(default)s)")
parser.add_argument("-q", "--quiet", action = "store_true",
help = "Silences the normal outputs (default=%(default)s)")
args = parser.parse_args(args = argv)
if args.debug:
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))
print("Argument", args)
query_base = "billtrack50"
results = {}
for legislator in args.legislators:
if not args.quiet:
print("==========")
query = query_base + " " + legislator
if args.debug:
print(f"Search query: \"{query}\"")
search_result = search(query, num_results = 1).__next__()
if args.debug:
print(f"Search result url: \"{search_result}\"")
soup = None
with urllib.request.urlopen(search_result) as thepage:
soup = BeautifulSoup(thepage, "html.parser")
if soup is not None:
legislator_name = soup.title.text.split(" | ")[0]
legislator_number = soup.find_all("img","d-block")[0]['src'].split('/')[-1]
if not args.quiet:
print(f"Information for {legislator_name}:")
print(f"\tBill Track 50 ID: {legislator_number}")
print("\tSchedulers:")
staff_table = soup.find('div','tab-pane fade show','staff-tab', id='staff')
staff_list = staff_table.find_all('tr')
schedulers = get_schedulers_from_table(staff_list, quiet = args.quiet)
if len(schedulers) == 0:
if not args.quiet:
print("\t\t<none found>")
if not args.quiet:
print("")
results[legislator] = schedulers
else:
raise RuntimeError(f"Unable to open the bill Track 50 page for {legislator}.")
return results
if __name__ == "__main__":
scrape_bill_track_50()