Python-美丽的汤-删除标签 [英] Python - Beautiful Soup - Remove Tags

查看:54
本文介绍了Python-美丽的汤-删除标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用Beautiful Soup将以下基于网络的数据提取为列表.在原始网站上是一张数字表:

I have extracted the below web based data as a list using Beautiful Soup. On the original website it's a table of numbers:

[<td class="right">113</td>, <td class="right">
 89 </td>, <td class="right last">
<b>117</b> </td>, <td class="right">113</td>, <td class="right">
 85 </td>, <td class="right last">
<b>114</b> </td>, <td class="right">100</td>, <td class="right">
 56 </td>, <td class="right last">
<b>84</b> </td>]

从此数据创建数字列表的最有效方法是什么?理想情况下,我想使用Beautiful Soup提取标签,但我无法从文档中弄清楚如何做到这一点.

What's the most efficient way to create a list of numbers from this data? Ideally I'd like to extract the tags using Beautiful Soup but I can't figure out how to do this from the documentation.

我原来的汤代码是:

print soup.find_all('td', 'right')             #printing this produces the above data

numbers_data = []                              #my attempt to extract tags
for e in soup.find_all('td', 'right'):
    numbers_data.append(e.extract())

print numbers_data

两者都返回相同的列表.

Both return the same list.

推荐答案

numbers_data = [int(e.text) for e in soup.find_all('td', 'right')]

print numbers_data

这篇关于Python-美丽的汤-删除标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆