Reputation: 345
From the following two lines of python code I get the following html output which belongs to class 'bs4.BeautifulSoup'
site_link = soup.find_all('a', string='Aberdeen')[0]
row = site_link.findParent('td').findParent('tr').findAll('td')
<html><body><p>[</p><td><a href="../networks/site-info?site_id=ABD">Aberdeen</a><br/>
<a class="smalltext" href="https://uk-air.defra.gov.uk/assets/graphs/ABD_weekly_m.png">Timeseries Graph</a></td>,
<td class="center"><span class="bg_low2 bold">48 (2 Low)</span></td>,
<td class="center"><span class="bg_low1 bold">4 (1 Low)</span></td>,
<td class="center"><span title="Not Measured">n/m</span></td>,
<td class="center"><span class="bg_low1 bold">2 (1 Low)</span></td>,
<td class="center"><span class="bg_low1 bold">6 (1 Low)
</span>
</td>,
<td>19/08/2017<br/>17:00:00</td>]</body></html>
How can I make it into a list whereby I can extract items so that, e.g. where my list is called mylist:
>>>print(mylist[1].text)
48 (2 Low)
Upvotes: 2
Views: 9025
Reputation: 402493
You're looking for tag.find_all
:
mylist = soup.find_all('span', class_='bg_low2 bold')
Now, mylist
contains all your span
tags, and you can access the ith span
's data with mylist[i].text
.
Upvotes: 3