-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.py
152 lines (109 loc) · 4.03 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
import re
import requests
import urllib
import urlparse
from bs4 import BeautifulSoup
from collections import OrderedDict
class PlanScraper(object):
def get_plans(self, zipcode, income, people):
plan_urls = self.get_plan_urls(zipcode, income, people)
return [self.get_plan(plan_url) for plan_url in plan_urls]
def get_plan(self, url):
response = requests.get(url)
soup = BeautifulSoup(response.content)
plan_name = soup.find_all('h2', class_='plan-name')[0].text
company, name = [s.strip() for s in plan_name.split(u'\xb7')]
plan_files = soup.find_all('ul', class_='plan-files')[0]
plan_link = plan_files.findAll('a')[0]['href']
plan = [
('url', url),
('company', company),
('name', name),
('link', plan_link),
]
details = soup.find_all('ol', class_='results')[0]
rows = details.find_all('div', class_='row')
plan.extend(self.parse_overview(rows[1]))
plan.extend(self.parse_premiums(rows[3]))
plan.extend(self.parse_details(rows[4]))
return OrderedDict(plan)
def parse_overview(self, row):
bs = row.find_all('b')
metal = bs[0].text.split(' ')[0]
plan_type = bs[1].text
lis = row.find_all('li')
network = lis[1].text if 3 == len(lis) else None
plan_id = lis[-1].text.split(':')[-1].strip()
return [
('metal', metal),
('plan_type', plan_type),
('network', network),
('plan_id', plan_id),
]
def parse_premiums(self, row):
ps = row.find_all('p')
premium = int(ps[0].text.strip('$'))
deductible = int(re.split(' |\n', ps[1].text)[1].strip('$'))
oop_max = int(re.split(' |\n', ps[2].text)[1].strip('$'))
return [
('premium', premium),
('deductible', deductible),
('oop_max', oop_max),
]
def parse_details(self, row):
details = []
for dd in row.find_all('dd'):
dd_text = dd.text.strip(' \n\t')
value = dd.find_all('span')[-1].text.strip(' \n\t')
key = dd_text[:-len(value)]
details.append((
key.strip(' \n\t'),
value
))
#print '**{}**'.format(span.text)
#print '**{}**'.format(span.parent.text)
#costs.append((
# span.parent.text[:-len(span.text)].strip(' \n\t'),
# span.text.strip(' \n\t')
#))
return details
def get_plan_urls(self, zipcode, income, people):
plan_urls = []
while True:
url = self.url(zipcode, income, people, start=len(plan_urls))
page_urls = list(self.get_page_urls(url))
if not len(page_urls):
break
plan_urls.extend(page_urls)
if len(page_urls) < 10:
break
return plan_urls
def get_page_urls(self, url):
response = requests.get(url)
soup = BeautifulSoup(response.content)
results = soup.findAll('ol', {'class': 'results'})[0]
for result in results.findAll('li'):
for a in result.findAll('a'):
if a.parent.name == 'h2':
yield urlparse.urljoin(url, a['href'])
def url(self, zipcode, income, people, start=0):
qs = [
('zip', zipcode),
('income', income),
]
for person in people:
qs.extend(self.person_qs(person).items())
if start:
qs.append(('start', start))
return 'https://www.healthcare.gov/see-plans/{}/results/?{}'.format(
zipcode,
urllib.urlencode(qs)
)
def person_qs(self, person):
return {
'age': person.age,
'mec': 1 if person.covered else '',
'parent': 1 if person.parent else '',
'smoker': 1 if person.smoker else '',
'pregnant': 1 if person.pregnant else '',
}