我正在制作一个 python 脚本来读取 NWS CAP 警报,并寻找龙卷风警告。我有足够远的脚本来阅读 XML,但是当龙卷风警告发出时我不能让它返回 true。这是因为我的脚本只会读取 XML 中的一个事件。我现在已经让它读取了所有的 XML,但是如果有龙卷风警告它不会返回 true,除非龙卷风警告是最后一个事件,因为它是如何循环的。任何人都可以在何时发出龙卷风警告时给我任何帮助,即使有其他警报,它也会返回真实?
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>