我正在尝试解析来自荷兰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?你知道吗
——按评论建议改为阅读
问题不在于XML解析;而是您错误地使用了} 返回一个类似文件的对象,该对象没有
response
对象。^{content
属性。相反,您应该调用read
:相关问题 更多 >
编程相关推荐