Select and store values between line-breaks into list

Viewed 45

I'm trying to select postcodes within line-breaks and storing them within a list. However, given the nature of the web-page I am finding it very difficult. I'm trying to assort this by hospital names, and gathering the hospital names is fine however there are many text between line-breaks so getting only the postcode is challenging.

Here is what I have tried:

l = ['http://www.wales.nhs.uk/ourservices/directory/Hospitals/92',
'http://www.wales.nhs.uk/ourservices/directory/Hospitals/62']

waleit = {'hospital':[],'address':[]}

asit = []

for url in l:
    soup = BeautifulSoup(requests.get(url).content, "lxml")
    for s in soup.find('div',{'style':'width:500px; float:left; '}):
        soupy = s.find_next('h1')
        try:
            waleit['hospital'].append(soupy.text)
        except AttributeError:
            continue
    wel = soup.find('div',{'style':'width:500px; float:left; '})
    for a in wel.childGenerator():
        x=[a]
        print(x[0])

which prints:

<h1>Bronglais General Hospital</h1>

        Caradoc Road, Aberystwyth
<br/>
 SY23 1ER
<br/>
<br/>

        Tel: 01970 623131
<br/>
 
<br/>
Type of Hospital: Major acute  -  Major A&E - Open 24 hours
<br/>

How do I extract for specific text with <br>...<br> like the postcode?

expected output:

{'hospital': ['Bronglais General Hospital', 'Glan Clwyd Hospital'],
 'address': ['SY23 1ER','LL18 5UJ']}
1 Answers

You are almost there. See below; note I used css selectors instead of find(); I just prefer them:

urls = ['http://www.wales.nhs.uk/ourservices/directory/Hospitals/92',
'http://www.wales.nhs.uk/ourservices/directory/Hospitals/62']

waleit = {'hospital':[],'address':[]}
hospitals,addresses = [],[]
for url in urls:
    req = requests.get(url)
    soup = BeautifulSoup(req.text,'lxml')
    data = list(soup.select_one("div[style='width:500px; float:left; ']").stripped_strings)
    hospitals.append(data[0])
    addresses.append(data[2])        
waleit['hospital']=hospitals
waleit['address']=addresses
waleit

Output:

{'hospital': ['Bronglais General Hospital', 'Glan Clwyd Hospital'],
 'address': ['SY23 1ER', 'LL18 5UJ']}
Related