gpt4 book ai didi

python - : in node causing Keyerror in xmlparsing using ElementTree

转载 作者:太空宇宙 更新时间:2023-11-03 19:37:48 24 4
gpt4 key购买 nike

您好,我正在使用 ElementTree 解析来自 Kuler 的 xml feed。我才刚刚开始使用 python,但我被困在这里了。解析工作正常,直到我尝试检索包含“:”的任何节点例如 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 年 3 月 30 日星期二 11:27:12(太平洋标准时间)

因此,如果我对每个项目的描述进行查找,我会得到很好的结果。但是当我尝试检索节点名中带有 : 的任何内容时,我得到了异常类型: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)

我确信这很简单,如果有人能指出我在这里做错了什么,我将不胜感激

谢谢基兰

最佳答案

基于此article (以及对本文的评论)我认为您必须用实际的 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 namespaces]
The element type represents a qualified name pair, also called universal name, as a string of the form “{uri}local“. This syntax can be used both for tag names and for attribute keys.

您还可以阅读此introduction ElementTree 如何处理命名空间。

关于python - : in node causing Keyerror in xmlparsing using ElementTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2549211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com