美丽的汤HTML提取

时间:2013-05-24 10:06:47

标签: python beautifulsoup

我正在努力获取我想要的数据,如果您知道如何使用BS,我确信它非常简单。在阅读完文档后,我一直试图将这个问题弄好几个小时。

目前我的代码在python中输出:

[<td>0.32%</td>, <td><span class="neg color ">&gt;-0.01</span></td>, <td>0.29%</td>, <td>0.38%</td>, <td><span class="neu">0.00</span></td>] 

我如何仅隔离不包含标签的td标签的内容?

即。我只想看0.32%,0.29%,0.38%。

谢谢。

import urllib2
from bs4 import BeautifulSoup

fturl = 'http://markets.ft.com/research/Markets/Bonds'
ftcontent = urllib2.urlopen(fturl).read()
soup = BeautifulSoup(ftcontent)

ftdata = soup.find(name="div", attrs={'class':'wsodModuleContent'}).find_all(name="td",       attrs={'class':''})

1 个答案:

答案 0 :(得分:2)

这是否适合您:

html_txt = """<td>0.32%</td>, <td><span class="neg color">
    &gt;-0.01</span></td>, <td>0.29%</td>, <td>0.38%</td>, 
    <td><span class="neu">0.00</span></td>
    """
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_txt)
print [tag.text for tag in soup.find_all('td') if tag.text.strip().endswith("%")]

输出是:

[u'0.32%', u'0.29%', u'0.38%']
相关问题