pythonhtmlbeautifulsoupnextsibling

Find next sibling at the same level if exists


I'm curious whether is it possible to get next sibling only when is there some in the same level.

<font face="Arial, Helvetica" size="2"><b>Current:</b>
        90
        <b>Increment : </b> 5<br>
        <b>End Date/Time : </b>06/08/2015 21:00<br>
        <b>Last Made at : </b><font size="1"><br></font>
<font ....

So the point is that sometimes there is a text after Last Made at : </b> but sometime there is not.

I do this:

soup.find('b',text='Last Made at : ').next_sibling

The problem is that if there is not another sibling after </b> tag, it returns some data from another <font> tag which I don't want.

I know that it would be possible to use font.contents but in my case would be the better option something like

`tag.next_sibling_in_the_same_level_or_None`

I didn't find the option so I'm asking whether is there something which could help me in this case.


Solution

  • is the last you do not want? if so see these. infact, the last is the sibling of the .you can prettify and print ,and you will see.

    <font face="Arial, Helvetica" size="2">
     <b>
      Current:
     </b>
     90
     <b>
      Increment :
     </b>
     5
     <br>
      <b>
       End Date/Time :
      </b>
      06/08/2015 21:00
      <br>
       <b>
        Last Made at :
       </b>
       <font size="1">
        <br/>
       </font>
       **<font>
        not need
       </font>**
      </br>
     </br>
    </font>
    

    if you html is

    <font face="Arial, Helvetica" size="2"><b>Current:</b>
            90
            <b>Increment : </b> 5<br>
            <b>End Date/Time : </b>06/08/2015 21:00<br>
            <b>Last Made at : </b><font size="1"><br></font>  
    **</font>**
    <font ....
    

    then,i think you will not get the last .