Python Beautiful Soup find string and extract following string

15,728

Once you find the correct tdwhich I presume is what you meant to have in place of a then get the next sibling with the class you want:

h = """<tr class="odd-row">
        <td>xyz</td>
        <td class="numeric">5,00%</td>
    </tr>
<tr class="even-row">
        <td>abc</td>
        <td class="numeric">50,00%</td
    </tr>
<tr class="odd-row">
        <td>ghf</td>
        <td class="numeric">2,50%</td>"""


from bs4 import BeautifulSoup

soup = BeautifulSoup(h)

for td in soup.find_all("td",text="abc"):
    print(td.find_next_sibling("td",class_="numeric"))

If the numeric td is always next you can just call find_next_sibling():

for td in soup.find_all("td",text="abc"):
    print(td.find_next_sibling())

For your input both would give you:

td class="numeric">50,00%</td>
Share:
15,728
Tessa
Author by

Tessa

Updated on June 04, 2022

Comments

  • Tessa
    Tessa almost 2 years

    I am programming a web crawler with the help of beautiful soup.I have the following html code:

    <tr class="odd-row">
            <td>xyz</td>
            <td class="numeric">5,00%</td>      
        </tr>
    <tr class="even-row">
            <td>abc</td>
            <td class="numeric">50,00%</td                      
        </tr>
    <tr class="odd-row">
            <td>ghf</td>
            <td class="numeric">2,50%</td>
    

    My goal is to write the numbers after class="numeric" to a specific variable. I want to do this conditional on the string above the class statement (e.g. "xyz", "abc", ...).

    At the moment I am doing the following:

    for c in soup.find_all("a", string=re.compile('abc')):
        abc=c.string
    

    But of course it returns the string "abc" and not the number in the tag afterwards. So basically my question is how to adress the string after class="numeric" conditional on the string beforehand.

    Thanks for your help!!!

  • Tessa
    Tessa almost 8 years
    Thanks! This works perfectly and is exactly what I wanted!