如果我有以下html结构,我该如何打印"打印这个"文本?
<div class="a">
<div>
<strong>
Skip this
</strong>
<span>
skip this
</span>
</div>
print this
</div>
由于
答案 0 :(得分:1)
您可以使用内容;
from bs4 import BeautifulSoup
soup = BeautifulSoup("""<div class="a">
<div>
<strong>
Skip this
</strong>
<span>
skip this
</span>
</div>
print this
</div>""")
# the text you need is the last element of the contents
soup.find('div', {'class': 'a'}).contents[-1].strip()
# u'print this'