Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中修改atom/rss提要?_Python_Feedparser - Fatal编程技术网

如何在python中修改atom/rss提要?

如何在python中修改atom/rss提要?,python,feedparser,Python,Feedparser,以下是我想在python中执行的操作: 吃点东西 向提要添加数据 重新发布这样修改的提要 Feedparser在解析方面做得很好,但它似乎没有提供从FeedParserDict对象组成XML文档的方法 除了编写自己的组合函数,还有更简单的替代方法吗?您可以使用lxml或使用Python模板库编写自己的函数 写下你自己的: 使用lxml的示例: 我使用Django模板引擎编写了自己的Atom 1.0序列化程序。我尽了最大努力使它尽可能完整,这样它就可以被其他人重复使用: import feedp

以下是我想在python中执行的操作:

  • 吃点东西
  • 向提要添加数据
  • 重新发布这样修改的提要
  • Feedparser在解析方面做得很好,但它似乎没有提供从
    FeedParserDict
    对象组成XML文档的方法


    除了编写自己的组合函数,还有更简单的替代方法吗?

    您可以使用lxml或使用Python模板库编写自己的函数

    • 写下你自己的:
    • 使用lxml的示例:

    我使用Django模板引擎编写了自己的Atom 1.0序列化程序。我尽了最大努力使它尽可能完整,这样它就可以被其他人重复使用:

    import feedparser
    from django.template import Template
    d = feedparser.parse("http://example.com/feed.xml") # creating a FeedParserDict
    # ... do stuff with d
    t = Template(the_stuff_below) # compiling the template
    output_atom_document = t.render(d)
    
    请注意,feedparser的卫生工作仅由
    feedparser.parse()
    完成,因此,如果需要,必须事先对喂给
    FeedParserDict
    的任何东西进行消毒

    如果您想使用它来修改提要,请记住您必须更改id值(一个按提要,一个按条目),因为这些值应该是唯一的

    <?xml version="1.0" encoding="{{ encoding }}"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
        <title type="{{ feed.title_detail.type }}" xml:lang="{{ feed.title_detail.language }}" xml:base="{{ feed.title_detail.base }}">{{ feed.title|escape }}</title>
    {% for link in feed.links %}
        <link rel="{{ link.rel }}" type="{{ link.type }}" href="{{ link.href }}" title="{{ link.title }}"/>
    {% endfor %}
        <subtitle type="{{ feed.subtitle_detail.type }}" xml:lang="{{ feed.subtitle_detail.language }}" xml:base="{{ feed.subtitle_detail.base }}">{{ feed.subtitle|escape }}</subtitle>
        <rights type="{{ feed.rights_detail.type }}" xml:lang="{{ feed.rights_detail.language }}" xml:base="{{ feed.rights_detail.base }}">{{ feed.rights|escape }}</rights>
        <generator uri="{{ feed.generator_detail.href }}" version="{{ feed.generator_detail.version }}">{{ feed.generator }}</generator>
        <info type="{{ feed.info_detail.type }}" xml:lang="{{ feed.info_detail.language }}" xml:base="{{ feed.info_detail.base }}">{{ feed.info|escape }}</info>
        <updated>{{ feed.updated }}</updated>
        <id>{{ feed.id }}</id>
        <author>
            <name>{{ feed.author }}</name>
            <uri>{{ feed.author_detail.href }}</uri>
            <email>{{ feed.author_detail.email }}</email>
        </author>
    {% for contributor in feed.contributors %}
        <contributor>
            <name>{{ contributor }}</name>
            <uri>{{ contributor_detail.href }}</uri>
            <email>{{ contributor_detail.email }}</email>
        </contributor>
    {% endfor %}
        <image>
            <title>{{ feed.image.title }}</title>
            <url>{{ feed.image.href }}</url>
            <link>{{ feed.image.link }}</link>
            <width>{{ feed.image.width }}</width>
            <height>{{ feed.image.height }}</height>
            <description>{{ feed.image.description }}</description>
        </image>
        <icon>{{ feed.icon }}</icon>
        <!-- not part of Atom 1.0: feed.textinput -->
        <!-- not part of Atom 1.0: feed.cloud -->
        <publisher>
            <name>{{ feed.publisher }}</name>
            <uri>{{ feed.publisher_detail.href }}</uri>
            <email>{{ feed.publisher_detail.email }}</email>
        </publisher>
    {% for tag in feed.tags %}
        <tag>
            <term>{{ tag.term }}</term>
            <scheme>{{ tag.scheme }}</scheme>
            <label>{{ tag.label }}</label>
        </tag>
    {% endfor %}
        <language>{{ feed.language }}</language>
        <!-- not part of Atom 1.0: feed.license -->
        <!-- not part of Atom 1.0: feed.errorreportsto -->
    {% for entry in entries %}
        <entry>
            <title type="{{ entry.title_detail.type }}" xml:lang="{{ entry.title_detail.language }}" xml:base="{{ entry.title_detail.base }}">{{ entry.title|escape }}</title>
        {% for link in entry.links %}
            <link rel="{{ link.rel }}" type="{{ link.type }}" href="{{ link.href }}" title="{{ link.title }}"/>
        {% endfor %}
            <summary type="{{ entry.summary_detail.type }}" xml:lang="{{ entry.summary_detail.language }}" xml:base="{{ entry.summary_detail.base }}">{{ entry.summary|escape }}</summary>
        {% for content in entry.content %}
            <content type="{{ content.type }}" xml:lang="{{ content.language }}" xml:base="{{ content.base }}">{{ content.value|escape }}</content>
        {% endfor %}
            <published>{{ entry.published }}</published>
            <updated>{{ entry.updated }}</updated>
            <created>{{ entry.created }}</created>
            <!-- not part of Atom 1.0: entry.expired -->
            <id>{{ entry.id }}</id>
            <author>
                <name>{{ entry.author }}</name>
                <uri>{{ entry.author_detail.href }}</uri>
                <email>{{ entry.author_detail.email }}</email>
            </author>
        {% for contributor in entry.contributors %}
            <contributor>
                <name>{{ contributor }}</name>
                <uri>{{ contributor_detail.href }}</uri>
                <email>{{ contributor_detail.email }}</email>
            </contributor>
        {% endfor %}
        {% for enclosure in entry.enclosures %}
            <link rel="enclosure" href="{{ enclosure.href }}" length="{{ enclosure.length }}" type="{{ enclosure.type }}"/>
        {% endfor %}
            <publisher>
                <name>{{ entry.publisher }}</name>
                <uri>{{ entry.publisher_detail.href }}</uri>
                <email>{{ entry.publisher_detail.email }}</email>
            </publisher>
        {% for tag in entry.tags %}
            <tag>
                <term>{{ tag.term }}</term>
                <scheme>{{ tag.scheme }}</scheme>
                <label>{{ tag.label }}</label>
            </tag>
        {% endfor %}
        <source>
            <author>
                <name>{{ entry.source.author }}</name>
                <uri>{{ entry.source.author_detail.href }}</uri>
                <email>{{ entry.source.author_detail.email }}</email>
            </author>
        {% for contributor in entry.source.contributors %}
            <contributor>
                <name>{{ contributor }}</name>
                <uri>{{ contributor_detail.href }}</uri>
                <email>{{ contributor_detail.email }}</email>
            </contributor>
        {% endfor %}
            <icon>{{ entry.source.icon }}</icon>
            <id>{{ entry.source.id }}</id>
        {% for link in entry.source.links %}
            <link rel="{{ link.rel }}" type="{{ link.type }}" href="{{ link.href }}" title="{{ link.title }}"/>
        {% endfor %}
            <logo>{{ entry.source.logo }}</logo>
            <rights type="{{ entry.source.rights_detail.type }}" xml:lang="{{ entry.source.rights_detail.language }}" xml:base="{{ entry.source.rights_detail.base }}">{{ entry.source.rights|escape }}</rights>
            <subtitle type="{{ entry.source.subtitle_detail.type }}" xml:lang="{{ entry.source.subtitle_detail.language }}" xml:base="{{ entry.source.subtitle_detail.base }}">{{ entry.source.subtitle|escape }}</subtitle>
            <title type="{{ entry.source.title_detail.type }}" xml:lang="{{ entry.source.title_detail.language }}" xml:base="{{ entry.source.title_detail.base }}">{{ entry.source.title|escape }}</title>
            <updated>{{ entry.source.updated }}</updated>
        </source>
        <!-- not part of Atom 1.0: entry.comments -->
        <!-- not part of Atom 1.0: entry.license -->
        </entry>
    {% endfor %}
        <!-- meaningless: version (this is Atom 1.0) -->
        <!-- meaningless: namespaces (namespace set to "http://www.w3.org/2005/Atom" in the <feed> tag) -->
        <!-- somewhere else: encoding (in the XML declaration) -->
        <!-- meaningless: status (HTTP status) -->
        <!-- meaningless: href (present if server redirect when fetching the original feed) -->
        <!-- meaningless: etag (part of HTTP headers) -->
        <!-- meaningless: modified (part of HTTP headers) -->
        <!-- meaningless: headers (HTTP headers) -->
        <!-- meaningless: bozo (set to 1 if not well-formed XML) -->
        <!-- meaningless: bozo_exception -->
    </feed>
    
    
    {{feed.title | escape}}
    {feed.links%中的链接为%s}
    {%endfor%}
    {{feed.subtitle | escape}}
    {{feed.rights | escape}}
    {{feed.generator}}
    {{feed.info | escape}
    {{feed.updated}
    {{feed.id}
    {{feed.author}
    {{feed.author_detail.href}
    {{feed.author_detail.email}
    {feed.contributors%中的参与者为%s}
    {{contributor}}
    {{contributor_detail.href}
    {{contributor_detail.email}
    {%endfor%}
    {{feed.image.title}
    {{feed.image.href}
    {{feed.image.link}
    {{feed.image.width}
    {{feed.image.height}
    {{feed.image.description}
    {{feed.icon}
    {{feed.publisher}
    {{feed.publisher_detail.href}
    {{feed.publisher_detail.email}
    {feed.tags%中的标记的%s}
    {{tag.term}}
    {{tag.scheme}}
    {{tag.label}}
    {%endfor%}
    {{feed.language}
    {entries%%中的条目的百分比}
    {{entry.title | escape}}
    {entry.links%中的链接为%s}
    {%endfor%}
    {{entry.summary | escape}}
    {entry.content%中的内容为%1}
    {{content.value | escape}
    {%endfor%}
    {{entry.published}
    {{entry.updated}
    {{entry.created}
    {{entry.id}
    {{entry.author}
    {{entry.author_detail.href}
    {{entry.author_detail.email}
    {entry.contributors%}
    {{contributor}}
    {{contributor_detail.href}
    {{contributor_detail.email}
    {%endfor%}
    {entry.enclosures%}
    {%endfor%}
    {{entry.publisher}
    {{entry.publisher_detail.href}
    {{entry.publisher_detail.email}
    {entry.tags%中标记的%s}
    {{tag.term}}
    {{tag.scheme}}
    {{tag.label}}
    {%endfor%}
    {{entry.source.author}
    {{entry.source.author_detail.href}
    {{entry.source.author_detail.email}
    {entry.source.contributors%中的参与者为%s}
    {{contributor}}
    {{contributor_detail.href}
    {{contributor_detail.email}
    {%endfor%}
    {{entry.source.icon}
    {{entry.source.id}
    {entry.source.links%中的链接为%s}
    {%endfor%}
    {{entry.source.logo}
    {{entry.source.rights | escape}}
    {{entry.source.subtitle | escape}}
    {{entry.source.title | escape}}
    {{entry.source.updated}
    {%endfor%}