Python 使用通用提要解析器读取RSS提要中的扩展元素集合
是否有任何方法可以使用读取扩展元素集合 这只是Kuler RSS提要中的一小段:Python 使用通用提要解析器读取RSS提要中的扩展元素集合,python,rss,adobe,feed,Python,Rss,Adobe,Feed,是否有任何方法可以使用读取扩展元素集合 这只是Kuler RSS提要中的一小段: <channel> <item> <!-- snip: regular RSS elements --> <kuler:themeItem> <kuler:themeID>123456</kuler:themeID> <!-- snip --> <kuler:themeS
<channel>
<item>
<!-- snip: regular RSS elements -->
<kuler:themeItem>
<kuler:themeID>123456</kuler:themeID>
<!-- snip -->
<kuler:themeSwatches>
<kuler:swatch>
<kuler:swatchHexColor>FFFFFF</kuler:swatchHexColor>
<!-- snip -->
</kuler:swatch>
<kuler:swatch>
<kuler:swatchHexColor>000000</kuler:swatchHexColor>
<!-- snip -->
</kuler:swatch>
</kuler:themeSwatches>
</kuler:themeItem>
</item>
</channel>
feed.entries[0]。kuler\u swatchhexcolor
只返回最后一个kuler:swatchhexcolor
。是否有任何方法可以使用feedparser
检索所有元素
我已经通过使用minidom解决了这个问题,但是如果可能的话,我想使用通用提要解析器(由于非常简单的API)。可以延长吗?我在文档中没有找到任何关于这方面的信息,因此如果有人对这个库有更多的经验,请告诉我。通用提要解析器对于大多数提要都很好,但是对于扩展提要,您可能想尝试一种叫做。它是一个XML/HTML/XHTML解析库,最初是为截屏而设计的;事实证明,这类事情也很精彩。文档非常好,它有一个自解释的API,所以如果您想使用其他任何东西,我建议您这样做 我可能会这样使用它:
>>> import BeautifulSoup
>>> import urllib2
# Fetch HTML data from url
>>> connection = urllib2.urlopen('http://kuler.adobe.com/path/to/rss.xml')
>>> html_data = connection.read()
>>> connection.close()
# Create and search the soup
>>> soup = BeautifulSoup.BeautifulSoup(html_data)
>>> themes = soup.findAll('kuler:themeitem') # Note: all lower-case element names
# Get the ID of the first theme
>>> themes[0].find('kuler:themeid').contents[0]
u'123456'
# Get an ordered list of the hex colors for the first theme
>>> themeswatches = themes[0].find('kuler:themeswatches')
>>> colors = [color.contents[0] for color in
... themeswatches.findAll('kuler:swatchhexcolor')]
>>> colors
[u'FFFFFF', u'000000']
所以你可能会认为这是一个非常酷的图书馆。如果你正在解析任何旧的RSS提要,那就不太好了,但是因为数据来自AdobeKuler,你可以非常确定它不会变化到足以破坏你的应用程序(也就是说,它是一个足够可信的源)
更糟糕的是试图解析Adobe该死的.ASE格式。我试着为它编写一个解析器,结果它变得非常可怕,非常快。Ug。因此,是的,RSS提要可能是与Kuler交互的最简单方式。谢谢,我也会检查一下。这个API似乎比minidom更容易:我会选择find/findAll vs.getElementsByTagName:)基于这个答案,我为kuler制作了一个小python API-pykuler:谢谢!
>>> import BeautifulSoup
>>> import urllib2
# Fetch HTML data from url
>>> connection = urllib2.urlopen('http://kuler.adobe.com/path/to/rss.xml')
>>> html_data = connection.read()
>>> connection.close()
# Create and search the soup
>>> soup = BeautifulSoup.BeautifulSoup(html_data)
>>> themes = soup.findAll('kuler:themeitem') # Note: all lower-case element names
# Get the ID of the first theme
>>> themes[0].find('kuler:themeid').contents[0]
u'123456'
# Get an ordered list of the hex colors for the first theme
>>> themeswatches = themes[0].find('kuler:themeswatches')
>>> colors = [color.contents[0] for color in
... themeswatches.findAll('kuler:swatchhexcolor')]
>>> colors
[u'FFFFFF', u'000000']