解析荷兰语NDW xm

2024-03-28 14:15:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解析来自荷兰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?你知道吗

——按评论建议改为阅读


Tags: inpyhttpreadnamesresponselinexml