Python XML提要解析器找不到特定事件
我正在制作一个python脚本来读取NWS CAP警报,并查找龙卷风警报。我有足够的脚本来读取XML,但是当发出龙卷风警告时,我无法让它返回真值。这是因为我的脚本只读取XML中的一个事件。我现在已经让它读取了所有XML,但是如果有龙卷风警告,它不会返回true,除非龙卷风警告是最后一个事件,因为它是如何循环的。有谁能给我任何帮助,无论何时龙卷风警报发出,即使有其他警报,它也会返回真的吗Python XML提要解析器找不到特定事件,python,xml,for-loop,feedparser,cap,Python,Xml,For Loop,Feedparser,Cap,我正在制作一个python脚本来读取NWS CAP警报,并查找龙卷风警报。我有足够的脚本来读取XML,但是当发出龙卷风警告时,我无法让它返回真值。这是因为我的脚本只读取XML中的一个事件。我现在已经让它读取了所有XML,但是如果有龙卷风警告,它不会返回true,除非龙卷风警告是最后一个事件,因为它是如何循环的。有谁能给我任何帮助,无论何时龙卷风警报发出,即使有其他警报,它也会返回真的吗 import feedparser import time next_check = time.time()
import feedparser
import time
next_check = time.time()
tornado = False
while True:
if time.time() > next_check:
url = 'http://127.0.0.1/test.xml'
feed = feedparser.parse(url)
for entry in feed.entries:
if entry.has_key("cap_event") is False:
tornado = False
else:
if entry.cap_event == "Tornado Warning":
tornado = True
else:
tornado = False
print("Tornado: " + str(tornado))
print("EVENT: " + entry.cap_event)
next_check = time.time() + 5
下面是一个XML示例来测试它
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>
<!--
This atom/xml feed is an index to active advisories, watches and warnings
issued by the National Weather Service. This index file is not the complete
Common Alerting Protocol (CAP) alert message. To obtain the complete CAP
alert, please follow the links for each entry in this index. Also note the
CAP message uses a style sheet to convey the information in a human readable
format. Please view the source of the CAP message to see the complete data
set. Not all information in the CAP message is contained in this index of
active alerts.
-->
<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>
<!-- TZN = <CDT> -->
<!-- TZO = <-5> -->
<!-- http-date = Mon, 04 May 2015 01:30:00 GMT -->
<id>https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&y=0</id>
<generator>NWS CAP Server</generator>
<updated>2015-05-03T20:30:00-05:00</updated>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Current Watches, Warnings and Advisories for Lancaster (NEC109) Nebraska Issued by the National Weather Service</title>
<link href='https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&y=0'/>
<entry>
<id>...</id>
<updated>2015-05-03T20:30:00-05:00</updated>
<published>2015-05-03T20:30:00-05:00</published>
<author>...</author>
<title>...</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e"/>
<summary>...</summary>
<cap:event>Flood Warning</cap:event>
<cap:effective>2015-05-03T20:30:00-05:00</cap:effective>
<cap:expires>2015-05-03T21:00:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Immediate</cap:urgency>
<cap:severity>Extreme</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Gage; Johnson; Lancaster</cap:areaDesc>
<cap:polygon>
40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79
</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>031067 031097 031109</value>
<valueName>UGC</valueName>
<value>NEC067 NEC097 NEC109</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/</value>
</cap:parameter>
</entry>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e</id>
<updated>2015-05-03T20:30:00-05:00</updated>
<published>2015-05-03T20:30:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Tornado Warning issued May 03 at 8:30PM CDT until May 03 at 9:00PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e"/>
<summary>THE NATIONAL WEATHER SERVICE IN OMAHA HAS ISSUED A * TORNADO WARNING FOR... NORTHWESTERN JOHNSON COUNTY IN SOUTHEASTERN NEBRASKA... NORTHEASTERN GAGE COUNTY IN SOUTHEASTERN NEBRASKA... SOUTHEASTERN LANCASTER COUNTY IN SOUTHEASTERN NEBRASKA... * UNTIL 900 PM CDT</summary>
<cap:event>Tornado Warning</cap:event>
<cap:effective>2015-05-03T20:30:00-05:00</cap:effective>
<cap:expires>2015-05-03T21:00:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Immediate</cap:urgency>
<cap:severity>Extreme</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Gage; Johnson; Lancaster</cap:areaDesc>
<cap:polygon>40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>031067 031097 031109</value>
<valueName>UGC</valueName>
<value>NEC067 NEC097 NEC109</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/</value>
</cap:parameter>
</entry>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7</id>
<updated>2015-05-03T20:08:00-05:00</updated>
<published>2015-05-03T20:08:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Severe Thunderstorm Warning issued May 03 at 8:08PM CDT until May 03 at 8:45PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7"/>
<summary>THE NATIONAL WEATHER SERVICE IN OMAHA HAS ISSUED A * SEVERE THUNDERSTORM WARNING FOR... SOUTHEASTERN LANCASTER COUNTY IN SOUTHEASTERN NEBRASKA... SOUTHWESTERN OTOE COUNTY IN SOUTHEASTERN NEBRASKA... * UNTIL 845 PM CDT * AT 808 PM CDT...A SEVERE THUNDERSTORM WAS LOCATED OVER PANAMA...OR</summary>
<cap:event>Severe Thunderstorm Warning</cap:event>
<cap:effective>2015-05-03T20:08:00-05:00</cap:effective>
<cap:expires>2015-05-03T20:45:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Immediate</cap:urgency>
<cap:severity>Severe</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Lancaster; Otoe</cap:areaDesc>
<cap:polygon>40.53,-96.6 40.67,-96.57 40.67,-96.32 40.52,-96.33 40.52,-96.56 40.53,-96.6</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>031109 031131</value>
<valueName>UGC</valueName>
<value>NEC109 NEC131</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.NEW.KOAX.SV.W.0045.150504T0108Z-150504T0145Z/</value>
</cap:parameter>
</entry>
</feed>
https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&y=0
NWS CAP服务器
2015-05-03T20:30:00-05:00
w-nws。webmaster@noaa.gov
国家气象局为内布拉斯加州兰开斯特(NEC109)发布的最新观测、警告和咨询
...
2015-05-03T20:30:00-05:00
2015-05-03T20:30:00-05:00
...
...
...
洪水警报
2015-05-03T20:30:00-05:00
2015-05-03T21:00:00-05:00
实际的
警觉的
遇见
立即的
极端
观察
量规;约翰逊;兰开斯特
40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79
图6
031067 031097 031109
教资会
NEC067 NEC097 NEC109
VTEC
/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/
http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e
2015-05-03T20:30:00-05:00
2015-05-03T20:30:00-05:00
w-nws。webmaster@noaa.gov
美国东部时间5月3日晚上8:30发布龙卷风警报,直到美国东部时间5月3日晚上9:00由NWS发布
奥马哈国家气象局发布了龙卷风警报,警告。。。内布拉斯加州东南部约翰逊县西北部。。。内布拉斯加州东南部的盖奇县东北部。。。内布拉斯加州东南部兰开斯特县东南部…*至晚上九时
龙卷风警报
2015-05-03T20:30:00-05:00
2015-05-03T21:00:00-05:00
实际的
警觉的
遇见
立即的
极端
观察
量规;约翰逊;兰开斯特
40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79
图6
031067 031097 031109
教资会
NEC067 NEC097 NEC109
VTEC
/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/
http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7
2015-05-03T20:08:00-05:00
2015-05-03T20:08:00-05:00
w-nws。webmaster@noaa.gov
新气象局于5月3日CDT晚上8:08发布严重雷暴警告,至5月3日CDT晚上8:45发布
奥马哈的国家气象局发布了一个*强雷暴警报,警告。。。内布拉斯加州东南部兰开斯特县东南部。。。内布拉斯加州东南部奥托县西南部…*直到CDT晚上845点*CDT晚上808点…一场严重的雷暴在巴拿马上空…或
严重雷暴警告
2015-05-03T20:08:00-05:00
2015-05-03T20:45:00-05:00
实际的
警觉的
遇见
立即的
严峻的
观察
兰开斯特奥托
40.53,-96.6 40.67,-96.57 40.67,-96.32 40.52,-96.33 40.52,-96.56 40.53,-96.6
图6
031109 031131
教资会
NEC109 NEC131
VTEC
/O.NEW.KOAX.SV.W.0045.150504T0108Z-150504T0145Z/
如果我正确理解您的问题,希望这是一个快速解决方案
我对这个问题的理解是,您希望使用feedparser检查xml文件,看看它是否包含以下xml条目:
<cap:event>Tornado Warning</cap:event>
希望能有帮助 很乐意帮忙!干杯
import feedparser
import time
next_check = time.time()
#tornado = False
while True:
if time.time() > next_check:
feed = feedparser.parse(r'/home/tomnl/test.xml')
# if tornado is declared here it means the file is checked a fresh
# each time
tornado = False
for entry in feed.entries:
if entry.cap_event == "Tornado Warning":
tornado = True
print("Tornado: " + str(tornado))
print("EVENT: " + entry.cap_event)
next_check = time.time() + 5
print("FINAL tornado", tornado)