【问题标题】:Beautiful Soup HTML Extraction美丽的汤 HTML 提取
【发布时间】:2013-05-24 10:06:47
【问题描述】:

我正在努力获取我想要的数据,如果您知道如何使用 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':''})

【问题讨论】:

  • 请向我们展示您迄今为止的尝试。

标签: python beautifulsoup


【解决方案1】:

这适合你吗:

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%']

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2020-01-19
    • 2021-04-03
    • 2016-09-11
    • 2012-12-19
    • 2015-05-08
    • 2018-09-13
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多