-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHSTrustsInfo-BS.py
59 lines (47 loc) · 1.96 KB
/
NHSTrustsInfo-BS.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
import requests
from bs4 import BeautifulSoup
import time
import csv
trust_url = 'https://www.nhs.uk/ServiceDirectories/Pages/NHSTrustListing.aspx'
res = requests.get(trust_url)
soup = BeautifulSoup(res.text, 'lxml')
all_trusts = [x for x in soup('a') if x['href'].startswith('/Services/Trusts/Overview/DefaultView.aspx?id=')]
all_items = []
for t in all_trusts:
trust_name = t.text
print(trust_name)
trust_site = 'https://www.nhs.uk' + t['href'].replace('Overview', 'HospitalsAndClinics')
res = requests.get(trust_site)
soup = BeautifulSoup(res.text, 'lxml')
items = [x for x in soup.find_all('div', {'class': 'panel-content'}) if 'Address' in str(x)]
for i in items:
item_name = i.find('h3')
if item_name:
item_name = item_name.text
else:
continue
if not i.find('a'):
continue
if i.find('a')['href'].startswith('/Services'):
url = 'https://www.nhs.uk' + i.find('a')['href']
service_type = i.find('a')['href'].split('/')[2].title()
else:
url = i.find('a')['href']
service_type = 'Other'
properties = [x.text for x in i.find('dl').find_all('dt')]
values = [BeautifulSoup(str(x).replace('<br/>', ', '), 'lxml').text for x in i.find('dl').find_all('dd')]
info_dict = {'Name': item_name,
'URL': url,
'Type': service_type,
'Trust Name': trust_name}
for i,k in enumerate(properties):
if k in ['PostCode', 'Ext', 'Website']:
continue
info_dict[k.strip(':')] = values[i]
all_items.append(info_dict)
time.sleep(2)
keys = ['Name', 'Trust Name', 'Type', 'Tel', 'Address', 'Email', 'URL']
with open('nhs_sites.csv', 'w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_items)