首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何使用python获取opendata(xml)数据

我如何使用python获取opendata(xml)数据
EN

Stack Overflow用户
提问于 2022-08-26 13:32:10
回答 1查看 62关注 0票数 1

我是python的新手,我目前正在做一个项目,我想问您我想使用python来读取这个URL的XML数据

网址:https://thbapp.thb.gov.tw/opendata/vd/one/VDLiveList.xml

字段描述:https://thbapp.thb.gov.tw/opendata/vd1.aspx

identification

  1. 显示每个VDID
  2. 的名称,每分钟读取XML并对流量进行15次之和(我需要每15分钟分析一次)
  3. ,当15分钟内的总数据量超过225时,会弹出一个提示(line )。
  4. 会找到一个可视化工具包来可视化这些数据,以便于使用。

下面是我的代码,请提供建议,谢谢!

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-26 13:46:29

由于文档中的标记是名称空间的( too.

  • Additionally,),因此您需要使用名称空间API.

  • Naturally,查询,您需要*/*//来执行对递归树的findall调用,而不是只查看根级别。在下面的示例中,我已经在本地下载了文档,但是您也可以从API.

  • Naturally,获得文档,而不是打印出来,例如,您希望将元组保存到例如,可以在.

中进行计算的SQL数据库。

代码语言:javascript
复制
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))

这个打印出来(例如)

代码语言:javascript
复制
('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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73501832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档