本文介绍了lxml和循环在python中创建xml rss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用lxml创建rss feed的xml.但是我在标签上遇到了麻烦,无法真正弄清楚如何添加动态数量的元素.鉴于lxml似乎只是将函数作为函数的参数,因此我似乎无法弄清楚如何在不重新生成整个页面的情况下为动态数量的项目循环.

I have been using lxml to create the xml of rss feed. But I am having trouble with the tags and cant really figure out how to to add a dynamic number of elements. Given that lxml seems to just have functions as parameters of functions, I cant seem to figure out how to loop for a dynamic number of items without remaking the entire page.

rss = page = (
      E.rss(
        E.channel(
          E.title("Page Title"),
   E.link(""),
   E.description(""),

            E.item(
                  E.title("Hello!!!!!!!!!!!!!!!!!!!!! "),
                  E.link("htt://"),
                  E.description("this is a"),
            ),
        )
      )
    )

推荐答案

channel = E.channel(E.title("Page Title"), E.link(""),E.description(""))
    for (title, link, description) in container:
        try:
                    mytitle = E.title(title)
                    mylink = E.link(link)
                    mydesc = E.description(description)
            item = E.item(mytitle, mylink, mydesc)
                except ValueError:
                    print repr(title)
                    print repr(link)
                    print repr(description)
                    raise
        channel.append(item)
    top = page = E.top(channel)

这篇关于lxml和循环在python中创建xml rss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:03
查看更多