Skip to main content
 首页 » 编程设计

Python/XML RSS Feedparser之空白字段

2025年05月04日9kuangbin

我正在尝试解析 NOAA Weather RSS Feed。我的脚本可以很好地提取条目,但有些字段是空白的,我不明白为什么。这似乎是提要中更复杂的数据类型的问题。这是代码:

url = 'http://alerts.weather.gov/cap/us.php?x=1' 
feed = feedparser.parse(url) 
print 'title: ', feed.entries[0].title 
print 'cap_geocode: ', feed.entries[0].cap_geocode 
print 'cap_event: ', feed.entries[0].cap_event 

这是打印条目的结果:

title:  Flood Watch issued June 18 at 5:00AM AKDT until June 21 at 4:00PM... 
cap_geocode:   
cap_event:  Flood Watch 

我不明白为什么 cap_geocode 是空白的。任何想法?这是只有一个元素的 xml 文件。提要实际上拉回了 100 或条目,因此我将其缩减为一个小样本。

<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' 
> 
 
<id>http://alerts.weather.gov/cap/us.atom</id> 
<logo>http://alerts.weather.gov/images/xml_logo.gif</logo> 
<generator>NWS CAP Server</generator> 
<updated>2014-06-18T08:22:00-06:00</updated> 
<author> 
<name>w-nws.webmaster@noaa.gov</name> 
</author> 
<title>Current Watches, Warnings and Advisories for the United States Issued by the National Weather Service</title> 
<link href='http://alerts.weather.gov/cap/us.atom'/> 
    <entry> 
    <id>http://alerts.weather.gov/cap/wwacapget.php?x=WY12515A62F544.WinterWeatherAdvisory.12515A71D320WY.RIWWSWRIW.8dcf1ff2519d541d79867824b0480d63</id> 
    <updated>2014-06-18T02:57:00-06:00</updated> 
    <published>2014-06-18T02:57:00-06:00</published> 
    <author> 
    <name>w-nws.webmaster@noaa.gov</name> 
    </author> 
    <title>Winter Weather Advisory issued June 18 at 2:57AM MDT until June 19 at 12:00AM MDT by NWS</title> 
    <link href='http://alerts.weather.gov/cap/wwacapget.php?x=WY12515A62F544.WinterWeatherAdvisory.12515A71D320WY.RIWWSWRIW.8dcf1ff2519d541d79867824b0480d63'/> 
    <summary>...WINTER WEATHER CONTINUES TO IMPACT THE WESTERN AND CENTRAL MOUNTAINS THROUGH MIDNIGHT.</summary> 
    <cap:event>Winter Weather Advisory</cap:event> 
    <cap:effective>2014-06-18T02:57:00-06:00</cap:effective> 
    <cap:expires>2014-06-19T00:00:00-06:00</cap:expires> 
    <cap:status>Actual</cap:status> 
    <cap:msgType>Alert</cap:msgType> 
    <cap:category>Met</cap:category> 
    <cap:urgency>Expected</cap:urgency> 
    <cap:severity>Minor</cap:severity> 
    <cap:certainty>Likely</cap:certainty> 
    <cap:areaDesc>Absaroka Mountains; Teton and Gros Ventre Mountains</cap:areaDesc> 
    <cap:polygon></cap:polygon> 
    <cap:geocode> 
    <valueName>FIPS6</valueName> 
    <value>056013 056017 056023 056029 056035 056039</value> 
    <valueName>UGC</valueName> 
    <value>WYZ002 WYZ012</value> 
    </cap:geocode> 
    <cap:parameter> 
    <valueName>VTEC</valueName> 
    <value>/O.CON.KRIW.WW.Y.0031.000000T0000Z-140619T0600Z/</value> 
    </cap:parameter> 
    </entry>     
</feed> 

如有任何帮助,我们将不胜感激。谢谢

*更新 我已验证 cap_geocode 是一个长度为 0 的空白 unicode 字符串,而不是某种对象。

请您参考如下方法:

看来您应该 monkeypatch feedparser ...不幸的是,xml 解析器似乎无法解析 cap_geocode 的属性。

我不是 xml 专家,但 ElementTree 似乎也没有解析 cap_geocode 的属性。

在某些时候,我停止了抗争,只是写道:

from bs4 import BeautifulSoup 
import requests 
 
url = 'http://alerts.weather.gov/cap/us.php?x=1' 
 
r = requests.get(url) 
soup = BeautifulSoup(r.text, 'xml') 
entries = soup.find_all('entry') 
 
geocode = {} 
k = None 
for tag in entries[0].geocode.children: 
    if tag.name == 'valueName': 
        k = tag.string 
    elif tag.name == 'value': 
        geocode[k] = tag.string 
 
print 'title: ', entries[0].title.string 
print 'cap_geocode: ', str(geocode) 
print 'cap_event: ', entries[0].event.string