将 FeedParser 对象序列化为 Atom

2024-03-28

我使用 feedparserhttp://www.feedparser.org/ http://www.feedparser.org/解析 Atom feed,并对生成的 Python 对象进行一些操作。之后,我想将对象序列化回 Atom。但 feedparser 似乎没有提供这样做的方法?

我注意到其他 Atom 库,例如 gdatahttp://code.google.com/p/gdata-python-client/ http://code.google.com/p/gdata-python-client/或民主派http://jtauber.com/demokritos/ http://jtauber.com/demokritos/但说实话,对于初学者来说似乎很难。我使用 feedparser 正是因为它极其简单。

根据 amaral 的良好回应,我用我最喜欢的模板语言 SimpleTAL 编写了一个序列化器

import feedparser

from simpletal import simpleTAL, simpleTALES, simpleTALUtils

mytemplate = """
<feed xmlns="http://www.w3.org/2005/Atom">
  <title tal:condition="feed/title" tal:content="feed/title"/>
  <link tal:condition="feed/link" tal:content="feed/link"/>
  <updated tal:condition="feed/updated" tal:content="feed/updated"/>
  <id tal:condition="feed/id" tal:content="feed/id"/>
  <!-- TODO other feed variables -->
  <entry xmlns='http://www.w3.org/2005/Atom'
       xmlns:thr='http://purl.org/syndication/thread/1.0'
       tal:repeat="entry entries">
    <title tal:condition="entry/title" tal:content="entry/title"/>
    <summary tal:condition="entry/summary" tal:content="entry/summary"/>
    <content tal:condition="entry/content" tal:content="python: entry.content[0]['value']"/> <!-- TODO: metadata and the other items in content -->
    <id tal:condition="entry/id" tal:content="entry/id"/>
    <published tal:condition="entry/published" tal:content="entry/published"/>
    <updated tal:condition="entry/updated" tal:content="entry/updated"/>
    <!-- TODO other entry fields -->
  </entry>
</feed>
"""
context = simpleTALES.Context(allowPythonPath=True)
template = simpleTAL.compileXMLTemplate (mytemplate)

class FeedParserPlus(feedparser.FeedParserDict):

    def serialize(self):
        context.addGlobal ("feed", self.feed)
        context.addGlobal ("entries", self.entries)
        result = simpleTALUtils.FastStringOutput()
        template.expand (context, result)
        return result.getvalue()

    @classmethod
    def parse(klass, text):
        result = feedparser.parse(text)
        return FeedParserPlus(result)

使用 Mako、Jinja 或 Django 等 Python 模板库生成提要相当容易。

使用 Bottle.py 的示例:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>{{! d['title'] }}</title>
    <subtitle>{{! d['subtitle'] }}</subtitle>
    <link rel="alternate" type="text/html" href="{{! d['site_url'] }}" />
    <link rel="self" type="application/atom+xml" href="{{! d['feed_url'] }}" />
    <id>{{! d['feed_url'] }}</id>
    <updated>{{! d['date_updated'] }}</updated>
    <rights>{{! d['copyright'] }}</rights>

    %for entry in entries:
    <entry>
        <title>{{! entry['title'] }}</title>
        <link rel="alternate" type="text/html" href="{{! entry['url'] }}" />
        <id>{{! entry['atom_id'] }}</id>
        <published>{{! entry['date_published'] }}</published>
        <updated>{{! entry['date_updated'] }}</updated>
        <author>
            <name>{{! d['author'] }}</name>
            <uri>{{! d['site_url'] }}</uri>
        </author>
        <content type="html" xml:base="{{! d['site_url'] }}" xml:lang="en">
            <![CDATA[{{! entry['body'] }}]]>
        </content>
    </entry>
    %end

</feed>

有关 Django 特别是 django-atompub 使用的更多信息:http://code.google.com/p/django-atompub/wiki/UserGuide http://code.google.com/p/django-atompub/wiki/UserGuide

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 FeedParser 对象序列化为 Atom 的相关文章

随机推荐

  • 最小化/小型化可可 NSWindow 没有标题栏

    我被困住了 显然 因为我在这里发布了一个问题 我为我的 OS X cocoa 应用程序构建了自己的自定义窗口控件 关闭按钮效果很好 没问题 当我禁用标题栏时 最小化按钮根本不起作用 因此 当标题栏像上图一样打开并且我点击此方法时 最小化效果
  • 查询谷歌电子表格的特定工作表

    我正在尝试使用谷歌电子表格作为临时数据库 我已按照以下教程中的说明进行操作 一切正常 http www alatechsource org blog 2012 05 using the google spreadsheets data ap
  • PHP:如何自动加载接口和摘要

    我有这个自动装载机要自动加载的类classes最初 但现在我想自动加载interfaces and abstracts以及 所以我做了以下改变answer https stackoverflow com questions 7924782
  • 在 PHP echo 中嵌入 javascript

    echo td manuf td 上面的这个行得通吗 我正在从 mysql 数据库中提取结果来编辑内容 但需要 jQuery 功能来编辑它 因此嵌入了 javascript 变量 EDIT 抱歉 缺乏上下文 它与我在这里提出的另一个问题有关
  • 无法使用 ggsurvplot 从列表中使用 survfit 对象绘制 kaplan-meier 曲线

    我正在尝试使用 survminer 包中的 ggsurvplot 绘制 Kaplan Meyer 曲线 当我传递保存在列表中的 survfit 对象时 我无法绘制它 让我以肺部数据集为例 一切正常如下 library survival li
  • Vulkan 管道顶部/底部和 ALL_COMMANDS

    作为很多 初学者 我认为使用 TOP OF PIPELINE 作为 dst 和 BOTTOM OF PIPELINE 作为 src 意味着两者的 ALL COMMANDS Here https github com KhronosGroup
  • Grails 2.0 中带有新 where 查询的参数

    在 Grails 2 0 中定义 where 查询时是否可以使用参数 例如 def query Book where id it Book sub query find 5 我尝试运行该代码 但它在调用 find 时抛出 MissingMe
  • 创建一个包含比原始元素更多元素的 ReactiveUI 派生集合

    是否可以创建一个 ReactiveUI 派生集合 其中包含比原始集合更多的元素 我已经看到有一种方法可以过滤集合并选择单个属性 但我正在寻找相当于可枚举的 SelectMany 操作 为了说明这一点 想象一下尝试获取代表每个陷入交通拥堵的乘
  • 避免 printf() 中的尾随零

    我一直在发现 printf 系列函数的格式说明符 我想要的是能够打印小数点后最大给定位数的双精度 或浮点数 如果我使用 printf 1 3f 359 01335 printf 1 3f 359 00999 I get 359 013 35
  • C# 中图片框的图像之间的转换[重复]

    这个问题在这里已经有答案了 可能的重复 Windows 窗体图片框中的图像转换 https stackoverflow com questions 3270919 transition of images in windows forms
  • 当数据回发时,MVC 如何填充模型

    MVC对于如何将数据发送到浏览器非常清楚 您访问一个 URL 它运行代码来创建模型 将该类型化模型传递到视图中 然后视图根据模型的状态呈现 HTML 然而 我发现不太清楚的是 当用户在页面上提交表单时 MVC 如何将该表单映射回模型以在控制
  • 连接字符串中包含特殊字符的密码

    我需要从 ASP NET 应用程序连接到我的 Dynamics CRM 365 本地实例 我的问题是连接帐户的密码如下 T jL4O vc t 30
  • Service Fabric 重启应用程序

    我有一个在启动时从 KeyVault 读取的服务结构应用程序 当我们更改 KeyVault 值时 我们必须重新启动节点才能读取新值 这会导致同一节点上的其他应用程序出现故障 我正在尝试编写一个 PowerShell 脚本来重新启动服务结构应
  • iOS 8 上应用内购买失败,提示用户详细信息不正确

    我们有一个带有应用内购买功能的应用程序 该应用程序在 iOS 7 上运行良好 但在 iOS 8 上 当用户尝试在应用程序中购买任何内容时 应用程序内购买会失败 并显示错误 您输入的 Apple ID 无法 找不到或您的密码不正确 请重试 即
  • __libc_start_main@plt 如何工作?

    为了研究目标文件在linux中是如何加载和运行的 我制作了最简单的c代码 文件名为simple c int main 接下来 我创建目标文件并将目标文件另存为文本文件 gcc simple c objdump xD a out gt sim
  • 使用 Perl6 语法解析二进制结构

    使用 Perl6 解析二进制结构的最佳选择是什么 在 Perl5 中 我们在 Perl6 上有 pack unpack 方法 它们似乎是实验性的 是否可以使用 Perl6 语法来解析二进制数据假设我有一个文件 其中包含以下二进制格式的记录
  • Qt 加载指示器通过动画图像(又名预加载器)还是替代方案?

    我想在加载时在我的表格视图上显示动画加载程序图像 下面的截图显示了一个印象 我为此使用了一个动画 gif 由setStyleSheet作为居中的背景图像 我面临两个问题 gif 已显示 但不是动画 是否可以显示动画 gif 作为背景图像 通
  • Celery 错误“没有这样的传输:amqp”

    Celery 工作正常 有一天命令行工作程序无法启动 并显示以下跟踪 Traceback most recent call last File home buildslave venv bin celery line 9 in
  • 如何使用 Cloudinary 将经过转换的 url 输出为字符串?

    我将在开头说这可能是错误的做法 我想做的是使用 url w transformation 到 JSdata 属性 目前 我正在使用以下内容来生成图像标签 cl image tag image asset filename to s tran
  • 将 FeedParser 对象序列化为 Atom

    我使用 feedparserhttp www feedparser org http www feedparser org 解析 Atom feed 并对生成的 Python 对象进行一些操作 之后 我想将对象序列化回 Atom 但 fee