【问题标题】:Python XML Feed parser cant find specific eventPython XML Feed解析器找不到特定事件
【发布时间】:2015-05-04 17:54:36
【问题描述】:

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

【问题讨论】:

    标签: python xml for-loop feedparser cap


    【解决方案1】:

    如果我理解你的问题是正确的,这应该是一个快速的解决方案。

    我对这个问题的理解是,您希望使用 feedparser 检查一个 xml 文件,看看它是否包含以下 xml 条目:

    <cap:event>Tornado Warning</cap:event>
    

    如果它在文档中的任何位置执行,您希望将变量 tornado 存储为 True。

    (您提到您想“返回”true,但由于您不在函数中,我推测您的意思只是将变量更改为 True)

    基本上,您只需要删除语句的 else 部分。所以它是这样的:

    (我删除了 'if entry.has_key("cap_event") is False:' 因为我认为它也可能是多余的)

    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)
    

    希望对您有所帮助。

    【讨论】:

    • 乐于助人!干杯。
    猜你喜欢
    • 1970-01-01
    • 2013-09-21
    • 2011-05-21
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2014-02-05
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多