Julio Cáceres Julio Cáceres - 1 month ago 11
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

find_all
method from Beautifulsoup. The thing is that I need to get some text before each table which is under the
h2
tag. That's why I'm using
soup.find_all(['table','h2'])
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")

Answer

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()
    print(header)
Comments