【发布时间】:2019-06-20 18:49:20
【问题描述】:
我正在尝试从该 url 解析一些 XML 数据:http://py4e-data.dr-chuck.net/comments_42.xml,返回 Count 值并对提取的值求和。
import urllib as ur
import xml.etree.ElementTree as ET
url = input(('Enter location: '))
print'Retrieving:', url
data = ur.urlopen(url).read()
tree = ET.fromstring(data)
counts = tree.findall('.//count')
print('Count: ', sum(counts))
#print('Sum: ', sum_all)
我知道这里存在一些基本问题,但我一直在尝试修改我的代码但没有成功。我收到如下类型错误:
Enter location: 'http://py4e-data.dr-chuck.net/comments_42.xml'
Retrieving: http://py4e-data.dr-chuck.net/comments_42.xml
Traceback (most recent call last):
File "extracting_xml.py", line 11, in <module>
print('Count: ', sum(counts))
TypeError: unsupported operand type(s) for +: 'int' and 'Element'
【问题讨论】:
-
请提供MCVE。特别是:您尝试解析的 XML 文档的摘录以及您遇到的错误。
标签: python xml python-3.x elementtree