0
<manifest>
    <item href="Text/Briefcontent.xhtml" id="Briefcontent.xhtml" media-type="application/xhtml+xml" />
    <item href="Text/Section115.xhtml" id="Section115.xhtml" media-type="application/xhtml+xml" />
 </manifest>

我需要这样的结果:key=id, value=href

dict={'Briefcontent.xhtml':'Text/Briefcontent.xhtml', 'Section115.xhtml':'Text/Section115.xhtml'}

我应该写什么python代码?</p>

4

1 回答 1

3

您可以使用BeautifulSoup

>>> from bs4 import BeautifulSoup as BS
>>> xml = """<manifest>
...     <item href="Text/Briefcontent.xhtml" id="Briefcontent.xhtml" media-type="application/xhtml+xml" />
...     <item href="Text/Section115.xhtml" id="Section115.xhtml" media-type="application/xhtml+xml" />
...  </manifest>"""
>>> soup = BS(xml, 'xml')
>>> d = {}
>>> for i in soup.findAll('item'):
...     idict = i.attrs
...     d[idict['id']] = idict['href']
>>> d
{u'Briefcontent.xhtml': u'Text/Briefcontent.xhtml', u'Section115.xhtml': u'Text/Section115.xhtml'}
于 2013-06-04T12:35:33.237 回答