Julio Cáceres Julio Cáceres - 4 months ago 31
Python Question

Filter by tag after find_all in BeautifulSoup

I'm currently trying to retrieve some info from some tables in a webpage. To do this I´m using the

method from Beautifulsoup. The thing is that I need to get some text before each table which is under the
tag. That's why I'm using
but I don't know how to retrieve the tag from the result (to determine if it´s a header or a table). I want something along this lines:

for tr in soup.find_all(['table','h2']):
if tr.tag='table':
print("table info")
elif tr.tag='h2':
print("header info")


Instead, use .find_previous_sibling() method to get to the previous h2 element of each table element:

for table in soup.find_all('table'): 
    header = table.find_previous_sibling("h2").get_text()