import bs4 html = r"<li> <span> 929</span> Serve Returned </li>" soup = bs4.BeautifulSoup(html) print soup.li.findAll(text=True, recursive=False)
This gives:
[u' ', u' Serve Returned ']
The first element is the βtextβ that you have before the passage. This method can help you find text before and after (and between) any child elements.
source share