我是python的新手,我目前正在做一个项目,我想问您我想使用python来读取这个URL的XML数据
网址:https://thbapp.thb.gov.tw/opendata/vd/one/VDLiveList.xml
字段描述:https://thbapp.thb.gov.tw/opendata/vd1.aspx
identification
下面是我的代码,请提供建议,谢谢!
import xml.etree.ElementTree as ET
import requests
url = "https://thbapp.thb.gov.tw/opendata/vd/one/VDLiveList.xml"
response = requests.get(url)
tree = ET.fromstring(response.text)
for vdid in tree.findall('VDLive'):
x = vdid.find('VDLive').text
print(x) 如何获得指定VDID的所有数据?例如: VD-11-0020-002-01
import xml.etree.ElementTree as ET
from urllib.request import urlopen
ns = {'xmlns': 'http://traffic.transportdata.tw/standard/traffic/schema/'}
response = urlopen('https://thbapp.thb.gov.tw/opendata/vd/one/VDLiveList.xml')
s = response.read().decode('utf-8')
root = ET.fromstring(s)
for vd in root.findall('.//xmlns:VDLives', ns):
vdid = vd.find('*/xmlns:VDLive/VDID[VD-11-0020-000-01]', ns)
print(vdid)发布于 2022-08-26 13:46:29
由于文档中的标记是名称空间的( too.
*/或*//来执行对递归树的findall调用,而不是只查看根级别。在下面的示例中,我已经在本地下载了文档,但是您也可以从API.
中进行计算的SQL数据库。
import xml.etree.ElementTree as ET
with open("VDLiveList.xml") as f:
tree = ET.parse(f).getroot()
for vdlive in tree.findall('*/{http://traffic.transportdata.tw/standard/traffic/schema/}VDLive'):
vdid = vdlive.find('{http://traffic.transportdata.tw/standard/traffic/schema/}VDID').text
data_collect_time = vdlive.find('{http://traffic.transportdata.tw/standard/traffic/schema/}DataCollectTime').text
for lane in vdlive.findall('*//{http://traffic.transportdata.tw/standard/traffic/schema/}Lane'):
lane_id = lane.find('{http://traffic.transportdata.tw/standard/traffic/schema/}LaneID').text
for vehicle in lane.findall('*//{http://traffic.transportdata.tw/standard/traffic/schema/}Vehicle'):
veh_vol = int(vehicle.find('{http://traffic.transportdata.tw/standard/traffic/schema/}Volume').text)
if veh_vol <= 0: # Invalid or uninteresting value
continue
veh_type = vehicle.find('{http://traffic.transportdata.tw/standard/traffic/schema/}VehicleType').text
print((vdid, lane_id, data_collect_time, veh_type, veh_vol))这个打印出来(例如)
('VD-11-0020-000-01', '1', '2022-08-26T21:32:00+08:00', 'S', 14)
('VD-11-0020-000-01', '2', '2022-08-26T21:32:00+08:00', 'S', 7)
('VD-11-0020-000-01', '0', '2022-08-26T21:32:00+08:00', 'S', 1)
('VD-11-0020-000-01', '1', '2022-08-26T21:32:00+08:00', 'S', 3)
('VD-11-0020-000-01', '2', '2022-08-26T21:32:00+08:00', 'S', 5)
('VD-11-0020-008-01', '1', '2022-08-26T21:32:00+08:00', 'S', 4)https://stackoverflow.com/questions/73501832
复制相似问题