2013-05-29 133 views
0

我的最终目标是将BeautifulSoup的ResultSet中添加了数到这里:编辑内容

[<span class="u">1,677</span>, <span class="u">114</span>, <span class="u">15</span>] 

<class 'BeautifulSoup.ResultSet'> 

因此,结束: 总和= 1806

但似乎就像通常用于迭代列表的技术在这里不起作用一样。 最后我知道我必须提取数字,删除逗号,然后添加它们。但我有点卡住,特别是提取数字。

真的很感谢一些帮助。谢谢

回答

1

看起来像通常的迭代技术正在为我工​​作。这里是我的代码:

from bs4 import BeautifulSoup 
# or `from BeautifulSoup import BeautifulSoup` if you are using BeautifulSoup 3 

text = "<html><head><title>Test</title></head><body><span>1</span><span>2</span></body></html>" 
soup = BeautifulSoup(text) 
spans = soup.findAll('span') 
total = sum(int(span.string) for span in spans) 
print(total) 
# 3 

你有什么试过的?你有没有任何错误信息可以与我们合作?