我正在使用这个pythonxml序列化库dexml。我不太明白如何将属性放在我从对象生成的xml中的一些标记上。我通读了文档,除非我看不懂,否则我找不到一个很好的解释。在
这是涉及的代码。在
import dexml
import urllib2
from dexml import fields
from bs4 import BeautifulSoup
class Section(dexml.Model):
section = fields.String()
entries = fields.List(fields.String(tagname="Entry"))
# Add something for href here, maybe?
class AtoZ(dexml.Model):
list = fields.List(Section)
def makeSoup(url):
return BeautifulSoup(urllib2.urlopen(url).read())
def main():
soup = makeSoup("http://www.somewebsite.com")
sectionList = []
# You might wonder about the length of this; I *could* split it up
# into variables to make it shorter. Also, the chaining is because
# the 'li' I want are only inside of a <ul class="Nav_fm>".
for li in soup.find('ul', {'class':"Nav_fm"}).find_all('li', {'class':"MenuLevel_0"}):
atzSection = Section()
atzSection.section = li.a.string
for innerLi in li.find_all('li', {'class':"MenuLevel_1"}):
atzSection.entries.append(innerLi.a.string)
# Somehow store innlerLi.a['href'] in atzSection
sectionList.append(atzSection)
atzList = AtoZ(list=sectionList)
f = open("C:\\atoz.xml", "w")
f.write(atzList.render(pretty=True))
f.close()
if __name__ == '__main__':
main()
这是生成的XML。在
^{pr2}$我希望每个<Entry>
都有{
重新定义章节条目要列出这样的条目:
看看dexmltest code-除了文档所描述的之外,还有很多关于如何使用它的好例子。在
相关问题 更多 >
编程相关推荐