Domanda

Ciao Sto usando ElementTree ad analizzare fuori un feed XML da Kuler. Sto solo a partire in Python, ma sono bloccato qui. L'analisi funziona bene fino a quando si tenta di recuperare tutti i nodi che contengono ':' per esempio Kuler: swatchHexColor

Di seguito è una versione ridotta del feed completo, ma stessa struttura:

<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>

   Tue, 30 Mar 2010 11:27:12 CET             

Quindi, se faccio un findall sulla voce descrizione di ogni articolo, ho capito che bene indietro. Ma minuto cerco di recuperare qualsiasi cosa con una: nel nome nodo ottengo Eccezione Tipo: KeyError Eccezione Valore: ':'

Quindi, questo funziona

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)

, ma questo non lo fa

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)

Sono sicuro che è una cosa semplice, se qualcuno mi potrebbe puntare a quello che sto facendo male qui sarei molto grato

grazie Kieran

È stato utile?

Soluzione

In base a questa articolo (e commenti su questo articolo) penso è necessario sostituire il nome dello spazio dei nomi con l'attuale URI (e rimuovere il colon e metterlo in {}):

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)
  

[namespace XML]
  Il tipo di elemento rappresenta una coppia nome qualificato, chiamato anche nome universale, come una stringa nella forma “ {uri} locale ”. Questa sintassi può essere utilizzato sia per i nomi dei tag e attributi per le chiavi.

Si può anche leggere in questo come ElementTree gestisce gli spazi dei nomi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top