Python/XML RSS Feedparser - 空白字段
我正在尝试解析NOAA天气的RSS源。我的脚本可以顺利提取条目,但有些字段是空的,我搞不清楚为什么。看起来这是因为源中的数据类型比较复杂。以下是我的代码:
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文件。实际上,这个源会返回数百个条目,所以我只取了一个小样本。
<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字符串,而不是某种对象。
2 个回答
0
所以,你的 cap_geocode 变量实际上指向了 RSS 源中一个有子节点的部分:
<cap:geocode>
<valueName>FIPS6</valueName>
<value>056013 056017 056023 056029 056035 056039</value>
<valueName>UGC</valueName>
<value>WYZ002 WYZ012</value>
</cap:geocode>
因此,你可能需要做一些稍微复杂的操作,比如这样 feed.entries[0].cap_geocode[0].valueName[0]
来获取 FIPS6
,用 feed.entries[0].cap_geocode[0].valueName[1]
来获取 UGC
,
用 feed.entries[0].cap_geocode[0].value[0]
来获取 056013 056017 056023 056029 056035 056039
,而
用 feed.entries[0].cap_geocode[0].value[1]
来获取 WYZ002 WYZ012
。
1
看起来你需要对 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