Search code examples
pythonweb-scrapingbeautifulsouphtml-parsing

Extracting text :after an element with Beautiful Soup


I would like to extract the text :after the <strong> element.

 <li data-toggle="tooltip" title="" data-original-title=""><strong>06:25</strong> &nbsp;vP</li>

I've tried the following

 medmar_live_departures_table = list(soup.select('li.tratta'))
 for li in medmar_live_departures_table:     
    info = li.text

but I'm getting both texts. I could use re to split the string but I was wondering if there was a more efficient and straightforward way of doing it.

Output
16:40  vP

Desired output
vP

Solution

  • You can get last text child node of each li as below:

    medmar_live_departures_table = soup.select('li.tratta')
    for li in medmar_live_departures_table :      
        info = [text for text in li.stripped_strings][-1]
        print(info)