[使用Atom / xml提要在python中使用feedparser标记值

问题描述 投票:0回答:1
我正在使用feedparser解析Atom / xml提要文件中的数据。该文件的链接是:

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>

我可以解析摘要,但是无法解析标签,例如,或者使用feedparser。这是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.entries [0]。['cap_event']]时,出现以下错误:

%运行feedparser2.py 文件“ /home/n8mdp/MyPythonApps/feedparser2.py”,第13行 打印(d ['entries'] [0]。['cap:event']) ^ SyntaxError:语法无效

如果我使用print(d.entries [0] ['cap_event'],则会收到以下错误:追溯(最近一次通话): 在第13行的文件“ /home/n8mdp/MyPythonApps/feedparser2.py” 打印(d ['entries'] [0] ['cap:event']) 文件“ /home/n8mdp/.thonny/Python36/lib/python3.6/site-packages/feedparser.py”,行356,位于[[getitem 返回字典。

getitem(自身,密钥)KeyError:'cap:event'

在Ubuntu 18.04.4 LTS中使用Thonny 2.1.16。 Feedparser已安装。

关于我如何在python中使用feedparser解析这些标签,有没有好的建议?

提前感谢!

python-3.x feedparser thonny
1个回答
0
投票
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是提要中条目数的索引。必须实际检查feedparser返回的提要以识别此提要。现在代码可以按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.