我是python新手,尝试申请一个公共交通信息的网站,然后我想在我的树莓皮小显示屏上显示出来。在
import request
xml = """<?xml version="1.0" encoding="UTF-8"?>
<Trias version="1.1" xmlns="http://www.vdv.de/trias" xmlns:siri="http://www.siri.org.uk/siri" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ServiceRequest>
<siri:RequestTimestamp>2016-06-27T13:34:00</siri:RequestTimestamp>
<siri:RequestorRef>EPSa</siri:RequestorRef>
<RequestPayload>
<StopEventRequest>
<Location>
<LocationRef>
<StopPointRef>8578169</StopPointRef>
</LocationRef>
</Location>
<Params>
<NumberOfResults>5</NumberOfResults>
<StopEventType>departure</StopEventType>
<IncludePreviousCalls>false</IncludePreviousCalls>
<IncludeOnwardCalls>false</IncludeOnwardCalls>
<IncludeRealtimeData>true</IncludeRealtimeData>
</Params>
</StopEventRequest>
</RequestPayload>
</ServiceRequest>
</Trias>"""
headers = {'Authorization': *'#MYCODE'*, 'Content-Type': 'application/xml'}
answer = requests.post('https://api.opentransportdata.swiss/trias', data=xml, headers=headers)
答案是什么:
^{pr2}$我现在如何继续从中获取一些信息?(对时间表时间和估计时间感兴趣)
我试着用ElementTree,但没用。在
提前谢谢!在
数据提供者的网站:https://opentransportdata.swiss/en/cookbook/departurearrival-display/
我认为@mzjn在他们提到:注意使用了XML名称空间。
为了防止问题是这样的,下面是一个使用ElementTree解析XML,同时正确处理默认名称空间的示例。在
我用了@andreacatano的答案作为基础。它产生完全相同的输出。在
Python
输出
^{pr2}$正如mzjn所说,你应该向我们提供更多关于你遇到的困难的信息。在
无论如何,如果您想解析xml,我建议使用第三方库来简化您的工作。在我的示例中,我使用了BeautifulSoup:
打印:
^{pr2}$如果您想坚持使用标准Python,可以使用:html.parser在
https://docs.python.org/3/library/html.parser.html
python还有很多第三方库可以让你更轻松地解析html)
相关问题 更多 >
编程相关推荐