If you have a look at the .contents
of a tag, you'll see that the text you want belongs to a class called NavigableString
.
from bs4 import BeautifulSoup, NavigableString
html = """<a aria-expanded="false" aria-owns="faqGen5" href="#">aaa <span class="nobreak">bbb</span> ccc?</a>"""
soup = BeautifulSoup(html, 'lxml')
for content in soup.find('a').contents:
print(content, type(content))
# aaa <class 'bs4.element.NavigableString'>
# <span class="nobreak">bbb</span> <class 'bs4.element.Tag'>
# ccc? <class 'bs4.element.NavigableString'>
Now, you simply need to get the elements belonging to the NavigableString
class and join them together.
text = ''.join([x for x in soup.find('a').contents if isinstance(x, NavigableString)])
print(text)
# aaa ccc?