使用Python解析HTML

时间:2017-02-05 22:57:03

标签: python html beautifulsoup html-parsing

我正在尝试使用Beautiful SOAP(Python库)解析HTML。有谁知道如何使用Beautiful SOAP解析HTML下面的内容?

  <span class="passingAlert bar">
     <span class="fold-buttons">
         <a href="#" onclick="fold();">Fold</a> | 
         <a href="#" onclick="unfold();">Unfold</a>
     </span>149 specs, 0 failed, 0 pending
  </span>

我需要获得149个规格,0个失败,0个来自HTML。

1 个答案:

答案 0 :(得分:1)

html = '''<span class="passingAlert bar">
     <span class="fold-buttons">
         <a href="#" onclick="fold();">Fold</a> | 
         <a href="#" onclick="unfold();">Unfold</a>
     </span>149 specs, 0 failed, 0 pending
  </span>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')

# get <span class="fold-buttons">
c = soup.find(class_="fold-buttons")

# get element after `span`
print( c.nextSibling.strip() )