您好我使用ElementTree的解析出从kuler的XML提要。我才刚刚开始在Python,但我在这里停留。 解析正常工作,直到我试图检索包含任何节点“:” e.g的Kuler:swatchHexColor

下面是一个削减全饲料,但相同的结构的版本:

<rss xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:kuler="http://kuler.adobe.com/kuler/API/rss/" xmlns:rss="http://blogs.law.harvard.edu/tech/rss" version="2.0">
 <channel>
 <title>kuler popular themes</title>
 <item>
 <title>Theme Title: Fresh Money</title>
 <description> 
 &lt;img src="http://kuler-api.adobe.com/kuler/themeImages/theme_808366.png" /&gt;&lt;br /&gt;

 Artist: thesylph005&lt;br /&gt;
 ThemeID: 808366&lt;br /&gt;
 Posted: 03/02/2010&lt;br /&gt;

 Hex:
 2F400D, 8CBF26, A8CA65, E8E5B0, 419184
</description>
<kuler:themeItem>
<kuler:themeID>808366</kuler:themeID>
<kuler:themeTitle>Fresh Money</kuler:themeTitle>
<kuler:themeImage>http://kuler-api.adobe.com/kuler/themeImages/theme_808366.png</kuler:themeImage>
<kuler:themeAuthor>
 <kuler:authorID>370750</kuler:authorID>
 <kuler:authorLabel>thesylph005</kuler:authorLabel>
</kuler:themeAuthor>
<kuler:themeTags/>
<kuler:themeRating>4</kuler:themeRating>
<kuler:themeDownloadCount>708</kuler:themeDownloadCount>
<kuler:themeCreatedAt>20100302</kuler:themeCreatedAt>
<kuler:themeEditedAt>20100302</kuler:themeEditedAt>
<kuler:themeSwatches>
 <kuler:swatch>
  <kuler:swatchHexColor>2F400D</kuler:swatchHexColor>
  <kuler:swatchColorMode>rgb</kuler:swatchColorMode>
  <kuler:swatchChannel1>0.183333</kuler:swatchChannel1>
  <kuler:swatchChannel2>0.25</kuler:swatchChannel2>
  <kuler:swatchChannel3>0.05</kuler:swatchChannel3>
  <kuler:swatchChannel4>0.0</kuler:swatchChannel4>
  <kuler:swatchIndex>0</kuler:swatchIndex>
 </kuler:swatch>
 <kuler:swatch>
  <kuler:swatchHexColor>8CBF26</kuler:swatchHexColor>
  <kuler:swatchColorMode>rgb</kuler:swatchColorMode>
  <kuler:swatchChannel1>0.55</kuler:swatchChannel1>
  <kuler:swatchChannel2>0.75</kuler:swatchChannel2>
  <kuler:swatchChannel3>0.15</kuler:swatchChannel3>
  <kuler:swatchChannel4>0.0</kuler:swatchChannel4>
  <kuler:swatchIndex>1</kuler:swatchIndex>
 </kuler:swatch>
 <kuler:swatch>
  <kuler:swatchHexColor>A8CA65</kuler:swatchHexColor>
  <kuler:swatchColorMode>rgb</kuler:swatchColorMode>
  <kuler:swatchChannel1>0.659722</kuler:swatchChannel1>
  <kuler:swatchChannel2>0.791667</kuler:swatchChannel2>
  <kuler:swatchChannel3>0.395833</kuler:swatchChannel3>
  <kuler:swatchChannel4>0.0</kuler:swatchChannel4>
  <kuler:swatchIndex>2</kuler:swatchIndex>
 </kuler:swatch>
 <kuler:swatch>
  <kuler:swatchHexColor>E8E5B0</kuler:swatchHexColor>
  <kuler:swatchColorMode>rgb</kuler:swatchColorMode>
  <kuler:swatchChannel1>0.91</kuler:swatchChannel1>
  <kuler:swatchChannel2>0.898047</kuler:swatchChannel2>
  <kuler:swatchChannel3>0.688705</kuler:swatchChannel3>
  <kuler:swatchChannel4>0.0</kuler:swatchChannel4>
  <kuler:swatchIndex>3</kuler:swatchIndex>
 </kuler:swatch>
 <kuler:swatch>
  <kuler:swatchHexColor>419184</kuler:swatchHexColor>
  <kuler:swatchColorMode>rgb</kuler:swatchColorMode>
  <kuler:swatchChannel1>0.254901</kuler:swatchChannel1>
  <kuler:swatchChannel2>0.57</kuler:swatchChannel2>
  <kuler:swatchChannel3>0.519034</kuler:swatchChannel3>
  <kuler:swatchChannel4>0.0</kuler:swatchChannel4>
  <kuler:swatchIndex>4</kuler:swatchIndex>
 </kuler:swatch>
</kuler:themeSwatches>

   星期二,2010 11点27分12秒的PST 03月30日             

所以,如果我做说每个项目的描述中的findall,我找回来的罚款。 不过分钟我尝试了获取任何东西:在节点名称我得到 异常类型:KeyError异常 异常值: ':'

因此,这作品

from elementtree.ElementTree import Element, SubElement, dump, parse
def xml():
    kulerurl = 'http://kuler-api.adobe.com/rss/get.cfm?listType=popular&startIndex=0&itemsPerPage=5&timeSpan=30&key=mykey'
    rss = parse(urllib.urlopen(kulerurl)).getroot()
    for element in rss.findall('channel/item'):
        print(element.findtext('description'))
    dump (rss)

但这并不

def xml():
    kulerurl = 'http://kuler-api.adobe.com/rss/get.cfm?listType=popular&startIndex=0&itemsPerPage=5&timeSpan=30&key=mykey'
    rss = parse(urllib.urlopen(kulerurl)).getroot()
    for element in rss.findall('channel/item/kuler:themeItem'):
        print(element.findtext('kuler:themeID'))
    dump (rss)

我敢肯定它的简单的东西,如果任何人都可以点我到我在做什么错在这里我将感激不尽。

感谢 基兰

有帮助吗?

解决方案

根据此文章(和关于本文的评论)我想您需要替换与实际URI命名空间名(和删除结肠并把它放在{}):

namespace = 'http://kuler.adobe.com/kuler/API/rss/'

def xml():
    kulerurl = 'http://kuler-api.adobe.com/rss/get.cfm?listType=popular&startIndex=0&itemsPerPage=5&timeSpan=30&key=mykey'
    rss = parse(urllib.urlopen(kulerurl)).getroot()
    for element in rss.findall('channel/item/{%s}themeItem' % namespace):
        print(element.findtext('{%s}themeID' % namespace))
    dump (rss)
  

[XML命名空间],点击   元素类型表示一个合格的名称对,也称通用名称,作为表单“的 {URI}本地”的字符串。该语法可以为标签名和属性为键均可使用。

您还可以阅读此介绍如何ElementTree的句柄命名空间。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top