如何跳过带有beautifulsoup的标签

时间:2017-05-31 03:00:48

标签: python web-scraping beautifulsoup

如果我有以下html结构,我该如何打印"打印这个"文本?

<div class="a">
 <div>
  <strong>
   Skip this
  </strong>
  <span>
   skip this
  </span>
 </div>
 print this
</div>

由于

1 个答案:

答案 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'