python - Python / XML 请求
问题描述
我是 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)
答案将是什么:
<?xml version="1.0" encoding="UTF-8"?>
<Trias xmlns="http://www.vdv.de/trias" version="1.1">
<ServiceDelivery>
<ResponseTimestamp xmlns="http://www.siri.org.uk/siri">2018-11-19T14:17:42Z</ResponseTimestamp>
<ProducerRef xmlns="http://www.siri.org.uk/siri">EFAController10.2.9.62-WIN-G0NJHFUK71P</ProducerRef>
<Status xmlns="http://www.siri.org.uk/siri">true</Status>
<MoreData>false</MoreData>
<Language>de</Language>
<DeliveryPayload>
<StopEventResponse>
<StopEventResult>
<ResultId>ID-8E6262DF-2FB8-4591-97A3-AC3E94E56635</ResultId>
<StopEvent>
<ThisCall>
<CallAtStop>
<StopPointRef>8578169</StopPointRef>
<StopPointName>
<Text>Basel, Thomaskirche</Text>
<Language>de</Language>
</StopPointName>
<ServiceDeparture>
<TimetabledTime>2018-11-19T14:16:00Z</TimetabledTime>
<EstimatedTime>2018-11-19T14:17:00Z</EstimatedTime>
</ServiceDeparture>
<StopSeqNumber>31</StopSeqNumber>
</CallAtStop>
</ThisCall>
<Service>
<OperatingDayRef>2018-11-19</OperatingDayRef>
<JourneyRef>odp:05036::H:j18:36143:36143</JourneyRef>
<LineRef>odp:05036::H</LineRef>
<DirectionRef>outward</DirectionRef>
<Mode>
<PtMode>bus</PtMode>
<BusSubmode>regionalBus</BusSubmode>
<Name>
<Text>Bus</Text>
<Language>de</Language>
</Name>
</Mode>
<PublishedLineName>
<Text>36</Text>
<Language>de</Language>
</PublishedLineName>
<OperatorRef>odp:823</OperatorRef>
<OriginStopPointRef>8589334</OriginStopPointRef>
<OriginText>
<Text>Basel, Kleinhüningen</Text>
<Language>de</Language>
</OriginText>
<DestinationStopPointRef>8588780</DestinationStopPointRef>
<DestinationText>
<Text>Basel, Schifflände</Text>
<Language>de</Language>
</DestinationText>
</Service>
</StopEvent>
</StopEventResult>
</StopEventResponse>
</DeliveryPayload>
</ServiceDelivery>
我现在怎样才能继续从中获取一些信息?(对 TimetabledTime 和 EstimatedTime 感兴趣)
我尝试使用 ElementTree 但它并没有真正起作用。
提前致谢!
数据提供者的网站:https ://opentransportdata.swiss/en/cookbook/departurearrival-display/
解决方案
我尝试使用 ElementTree 但它并没有真正起作用。
我认为@mzjn 在他们提到时可能是对的:注意使用了 XML 命名空间。
以防万一,这是一个使用 ElementTree 解析 XML 同时正确处理默认命名空间的示例。
我以@AndreaCattaneo 的答案为基础。它产生完全相同的输出。
Python
import xml.etree.ElementTree as ET
from datetime import datetime
test_answer = """<?xml version="1.0" encoding="UTF-8"?>
<Trias xmlns="http://www.vdv.de/trias" version="1.1">
<ServiceDelivery>
<ResponseTimestamp xmlns="http://www.siri.org.uk/siri">2018-11-19T14:17:42Z</ResponseTimestamp>
<ProducerRef xmlns="http://www.siri.org.uk/siri">EFAController10.2.9.62-WIN-G0NJHFUK71P</ProducerRef>
<Status xmlns="http://www.siri.org.uk/siri">true</Status>
<MoreData>false</MoreData>
<Language>de</Language>
<DeliveryPayload>
<StopEventResponse>
<StopEventResult>
<ResultId>ID-8E6262DF-2FB8-4591-97A3-AC3E94E56635</ResultId>
<StopEvent>
<ThisCall>
<CallAtStop>
<StopPointRef>8578169</StopPointRef>
<StopPointName>
<Text>Basel, Thomaskirche</Text>
<Language>de</Language>
</StopPointName>
<ServiceDeparture>
<TimetabledTime>2018-11-19T14:16:00Z</TimetabledTime>
<EstimatedTime>2018-11-19T14:17:00Z</EstimatedTime>
</ServiceDeparture>
<StopSeqNumber>31</StopSeqNumber>
</CallAtStop>
</ThisCall>
<Service>
<OperatingDayRef>2018-11-19</OperatingDayRef>
<JourneyRef>odp:05036::H:j18:36143:36143</JourneyRef>
<LineRef>odp:05036::H</LineRef>
<DirectionRef>outward</DirectionRef>
<Mode>
<PtMode>bus</PtMode>
<BusSubmode>regionalBus</BusSubmode>
<Name>
<Text>Bus</Text>
<Language>de</Language>
</Name>
</Mode>
<PublishedLineName>
<Text>36</Text>
<Language>de</Language>
</PublishedLineName>
<OperatorRef>odp:823</OperatorRef>
<OriginStopPointRef>8589334</OriginStopPointRef>
<OriginText>
<Text>Basel, Kleinhüningen</Text>
<Language>de</Language>
</OriginText>
<DestinationStopPointRef>8588780</DestinationStopPointRef>
<DestinationText>
<Text>Basel, Schifflände</Text>
<Language>de</Language>
</DestinationText>
</Service>
</StopEvent>
</StopEventResult>
</StopEventResponse>
</DeliveryPayload>
</ServiceDelivery>
</Trias>"""
ns = {"t": "http://www.vdv.de/trias"}
tree = ET.fromstring(test_answer)
# as strings
timetabled_time = tree.find(".//t:TimetabledTime", ns).text
estimated_time = tree.find(".//t:EstimatedTime", ns).text
# as datetime objects
date_format = "%Y-%m-%dT%H:%M:%SZ"
timetabled_time = datetime.strptime(timetabled_time, date_format)
estimated_time = datetime.strptime(estimated_time, date_format)
print("Timetabled time: {} at {}".format(timetabled_time.date(), timetabled_time.time()))
print("Estimated time: {} at {}".format(estimated_time.date(), estimated_time.time()))
输出
Timetabled time: 2018-11-19 at 14:16:00
Estimated time: 2018-11-19 at 14:17:00
推荐阅读
- java - GridView 代表同一个Activity中的文件管理器
- android - 手动绘制的图表上的频率不正确
- c# - 无法访问外部类型的非静态成员
- c# - 从 WPF 中的父 UserControl 中删除控件
- dart - 如何在 Flutter 的屏幕上获取小部件的绝对坐标?
- javascript - 在 php 的另一个页面中从脚本打开引导模式
- dart - setState() 重新渲染特定的 Widget。需要代码修复
- r - R中的标题/头部上方的情节
- python - 带有随机森林的 sklearn 校准分类器
- node.js - 发送邮件()上的Nodemailer w Google Mail服务帐户错误