简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM