使用 python 中的 Atom/xml 提要解析 <cap:event>、<cap:urgency> 标记值



我正在使用feedparser来解析来自Atom/xmlfeed文件的数据。该文件的链接是:

https://alerts.weather.gov/cap/oh.php?x=0

这是国家气象局发布的用于提供天气警报信息的提要。此源使用通用警报协议 (CAP( 警报消息。我正在尝试解析以下内容:

<summary>...AIR QUALITY ADVISORY IN EFFECT UNTIL MIDNIGHT EDT TONIGHT... The Miami Valley Regional Planning Commission and the Regional Air Pollution Control Agency have issued an Air Pollution and Air Quality Advisory for Montgomery, Miami, Greene, Clark, Preble and Darke counties in the Miami Valley Region, until midnight EDT tonight.</summary>
<cap:event>Air Quality Alert</cap:event>
<cap:effective>2020-06-08T15:15:00-04:00</cap:effective>
<cap:expires>2020-06-09T19:30:00-04:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Unknown</cap:urgency>
<cap:severity>Unknown</cap:severity>
<cap:certainty>Unknown</cap:certainty>
<cap:areaDesc>Clark; Darke; Greene; Miami; Montgomery; Preble</cap:areaDesc>

我可以解析出摘要,但我无法解析出标签,例如使用提要解析器。这就是我为Raspberry Pi项目所需要的。我尝试了许多不同的方法,例如:

d = feedparser.parse('http://alerts.weather.gov/cap/ms.php?x=0')
print (d.entries[0].['cap_event'])
print (d.entries[0]['cap:event'])

当我尝试打印(d.entryries[0].['cap_event'](,我收到以下错误:

%运行 feedparser2.py 文件 "/home/n8mdp/MyPythonApps/feedparser2.py",第 13 行 print (d['entryries'][0].['上限:事件']( ^ 语法错误:语法无效

如果我使用 print (d.entries[0]['cap_event'],它会得到以下错误: 回溯(最近一次调用(: 文件 "/home/n8mdp/MyPythonApps/feedparser2.py",第 13 行,在 print (d['entryries'][0]['cap:event']( 文件 "/home/n8mdp/.thonny/Python36/lib/python3.6/site-packages/feedparser.py",第 356 行,在 getitem returndict.getitem(self, key(键错误:"上限:事件">

在 Ubuntu 18.04.4 LTS 中使用 Thonny 2.1.16。已安装源解析器。

有没有人对如何在 python 中使用提要解析器解析这些标签有很好的建议?

提前感谢!

经过更深入的调查,我确定以下方法有效:

print (alertFeed['entries'][nws_entry_id]['id'])
print (alertFeed['entries'][nws_entry_id]['summary'])
print (alertFeed['entries'][nws_entry_id]['cap_event'])
print (alertFeed['entries'][nws_entry_id]['cap_urgency'])
print (alertFeed['entries'][nws_entry_id]['cap_areadesc'])

nws_entry_id 是源中条目数的索引。必须实际查看提要解析器返回的提要才能识别这一点。现在代码按预期工作。

最新更新