【发布时间】:2018-07-23 13:33:37
【问题描述】:
我最近开始学习python。现在我想从网站上删除数字来总结它们。
这是我的代码:
# read data -> extract numbers -> compute sum
import urllib.request, urllib.parse
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_42.html')
file = BeautifulSoup(html, 'html.parser')
tags = file('span')
calcs = 0
for tag in tags:
tag.decode()
calcs += int(tag.string)
print(calcs)
在第 11 行(calcs += ...)中,我不知道该怎么做,我在互联网的某个地方找到了 .string,它帮助我将数字排除在外,但我不太确定为什么这有效或 .string 的作用。我自己找不到任何有关这方面的信息来源。如果我将 .string 更改为 .int 它会得到 'None'
希望有人能解释一下.string的用法。
提前致谢。
【问题讨论】:
-
string 是一个成员变量,用于从标签中提取数据:stackoverflow.com/questions/25327693/…
标签: python url beautifulsoup numbers