Search code examples
pythonbeautifulsouphtml-tabletags

Beautiful Soup find a class next to a TD


Here the code


<tr class="even">
<td>**keyword** </td>
<td class="separator"></td>
<td>
<span title="blablabla" class="data-siloing">**Text I want**</span> </td>
</tr>

How can I get this text using beautifulsoup ?

Try to get the text between span tag with class after a TD knowed ?


Solution

  • You can try:

    from bs4 import BeautifulSoup
    
    html_text = '''\
    <tr class="even">
    <td>**keyword** </td>
    <td class="separator"></td>
    <td>
    <span title="blablabla" class="data-siloing">**Text I want**</span> </td>
    </tr>'''
    
    soup = BeautifulSoup(html_text, 'html.parser')
    
    # 1. find the td with "keyword"
    keyword_td = soup.find(lambda tag: tag.name == 'td' and 'keyword' in tag.text)
    
    # 2. find the next span tag
    span = keyword_td.find_next('span')
    
    # 3. get the text
    print(span.text.strip())
    

    Prints:

    **Text I want**