<h3>
<span></span>
<span class='headline'>Headline #1</span>
</h3>
<table class='striped'></table>
<h4>
<span class='headline'>Headline #2</span>
</h4>
<table class='striped'></table>
<p>
<span class='headline'>Headline #3</span>
</p>
<ul></ul>
<center>
<table class='striped'></table>
</center>
This is my structure. I am enumerating over the table tags and want to retrieve the text value of the span tags with a class of 'headline' which is nearest to my table. By "nearest" I mean that if you were to flatten out the html, I want to target the span with a class 'headline' that you would come across first if you started up from the point of the table
Sometimes those spans are nested inside an h3, sometimes an h4, sometimes a p tag. Sometimes the table tag is on the same level as the h3/h4/p and sometimes it is itself nested inside a center tag. And sometimes the h3/h4/p tag is an immediate sibling of the table and sometimes it isn't.
How can I use BeautifulSoup to find nearest span.headline regardless of nesting level and whether it is nested inside a parent or sibling?
So far I've got this code
tables = soup.findAll("table", {"class": ["striped"]})
for index, table in enumerate(tables):
headline = table.find_previous('h3').("span", {"class" : ["headline"]}).text