python - Converting BeautifulSoup object into list to extract data -


from following 2 lines of python code following html output belongs 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 make list whereby can extract items that, e.g. list called mylist:

>>>print(mylist[1].text) 48 (2 low) 

you're looking tag.find_all:

mylist = soup.find_all('span', class_='bg_low2 bold') 

now, mylist contains span tags, , can access ith span's data mylist[i].text.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -