【发布时间】:2017-05-11 02:03:00
【问题描述】:
我正在尝试从此链接获取“span”标签中的 数字:http://python-data.dr-chuck.net/comments_42.html
数据如下:
<tr><td>Modu</td><td><span class="comments">90</span></td></tr>
<tr><td>Kenzie</td><td><span class="comments">88</span></td></tr>
<tr><td>Hubert</td><td><span class="comments">87</span></td></tr>
代码如下:
import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# Retrieve all of the anchor tags
tags = soup('span')
numbers = [number.contents[0] for number in tags]
print numbers
当我打印numbers 时,它会显示如下内容:
[u'97', u'97', u'90', u'90', u'88', u'87', u'87', u'80', u'79', u'79', u'78', u'76', u'76', u'72', u'72', u'66', u'66', u'65', u'65', u'64', u'61', u'61', u'59', u'58', u'57', u'57', u'54', u'51', u'49', u'47', u'40', u'38', u'37', u'36', u'36', u'32', u'25', u'24', u'22', u'21', u'19', u'18', u'18', u'14', u'12', u'12', u'9', u'7', u'3', u'2']
为什么会得到那些你?我的目标是在没有 u 的情况下投射这些字符串并得到它们的总和。
【问题讨论】:
标签: python python-2.7 beautifulsoup urllib