【发布时间】:2018-01-15 23:02:57
【问题描述】:
我正在尝试解析来自荷兰 NDW 的 XML 文件,其中包含许多荷兰高速公路上每分钟的交通速度。我使用这个示例文件:http://www.ndw.nu/downloaddocument/e838c62446e862f5b6230be485291685/Reistijden.zip
我正在尝试使用 Python 解析变量中的旅行时间数据,但我很挣扎。
from xml.etree import ElementTree
import urllib2
url = "http://weburloffile.nl/ndw/Reistijden.xml"
response = urllib2.urlopen(url)
namespaces = {
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'a': 'http://datex2.eu/schema/2/2_0'
}
dom = ElementTree.fromstring(response.read)
names = dom.findall(
'soap:Envelope'
'/a:duration',
namespaces,
)
#print names
for duration in names:
print(duration.text)
我收到这个新错误
Traceback (most recent call last):
File "test.py", line 9, in <module>
dom = ElementTree.fromstring(response.read)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
parser.feed(text)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1651, in feed
self._parser.Parse(data, 0)
TypeError: Parse() argument 1 must be string or read-only buffer, not instancemethod
如何正确解析这个(复杂的)xml?
-- 按照评论的建议将其更改为已读
【问题讨论】:
标签: python performance xml-parsing traffic