【问题标题】:Python-generated RSS: outputting raw HTML?Python 生成的 RSS:输出原始 HTML?
【发布时间】:2014-04-21 00:20:23
【问题描述】:

我正在使用 PyRSS2Gen,我想在我的提要中发布包含每个项目的原始 HTML(特别是几张图片)。

但是,looking at the source 似乎 RSSItem 的构造函数不接受“图像”并且所有 HTML 都是自动转义的 - 有什么聪明的方法可以解决这个问题吗?

我找到了this post,但代码示例似乎不起作用。

如果有人有更好的解决方案,我不喜欢 PyRSS2Gen。也许我应该只写自己的 RSS 提要?

谢谢!

【问题讨论】:

    标签: python rss


    【解决方案1】:

    我从痛苦的经历中了解到 PyRSS2Gen 不是解决这个问题的方法。问题在于 PyRSS2Gen 使用 python 的 sax 库,特别是 saxutility.xmlwriter,它转义了 XML 中需要转义的所有字符,包括尖括号。所以即使你扩展 PyRSS2Gen 来添加一个标签,它仍然会出现问题。

    通常,我看到 RSS 中的 html(它是 XML,而不是 html)包装为 CDATA 部分。 Python 的 sax 库没有 CDATA 的概念,但 minidom 有。所以我所做的就是删除 PyRSS2Gen,添加一些我自己的额外代码行,并使用 minidom 生成 XML。

    你只需要来自 minidom 的 Document (from xml.dom.minidom import Document)

    你像这样构建文档:

    doc = Document()
    rss=doc.createElement('rss')
    rss.setAttribute('version', '2.0')
    doc.appendChild(rss)
    channel=doc.createElement('channel')
    rss.appendChild(channel)
    channelTitle=doc.createElement('title')
    channel.appendChild(channelTitle)
    

    等,然后在完成后生成 xml (RSS) 文件:

    f = open('whitegrass.xml', "w")
    doc.writexml(f)
    f.close()
    

    【讨论】:

    • 您的回答启发了我写RSS2Producer,它使用xml.dom.minidom 生成一个RSS 2.0 提要。这个包可以用pip install rss2producer安装。
    【解决方案2】:

    我是撰写您列出的博客文章的人。在安装 PyRSSGen2 之后,我从 gist 中复制了代码并在 Kubuntu 11.10 下运行它,并且生成了没有问题的代码。看一下 test.xml 文件,应该是这样的;

    <?xml version="1.0" encoding="utf-8"?>
        <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
        <channel>
            <title>Example Title</title>
            <link>http://example.com</link>
            <description>Example RSS Output</description>
            <pubDate>Thu, 27 Oct 2011 05:36:27 GMT</pubDate>
            <lastBuildDate>Thu, 27 Oct 2011 05:36:27 GMT</lastBuildDate>
            <generator>PyRSS2Gen-1.0.0</generator>
            <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    
            <item>
                <title>Item Title</title>
                <link>http://example.com</link>
                <media:thumbnail url="http://example.com/image.jpg"></media:thumbnail>
                <description>< ![CDATA[<p><b>example</b>text<p><br/>
        <p>Where are you going today?</p>
        ]]></description>
                <guid>random_guid_x9129319</guid>
                <pubDate>Thu, 27 Oct 2011 14:36:27 GMT</pubDate>
            </item>
        </channel>
        </rss>
    

    为了后代,我将尝试在下面解释该代码的工作原理。

    就像上面说的 ViennaMike 一样,PyRSS2Gen 使用内置的 SAX 库,它会自动转义 HTML。但是,有一些方法可以解决这个问题。在您提到的代码片段中,我覆盖了 PyRSS2Gen 的“RSSItem”,因此当它输出“描述”时,它实际上不会输出任何内容。 (这就是包含“NoOutput”类的原因)。

    由于没有输出描述,我们必须自己添加一个方法将其附加到输出。因此,“publish_extensions”代码(输出 media_thumbnail 和描述标签)。

    我可以看到它有些令人困惑(因为您不需要 media_thumbnail 类)所以我继续并重新编写了该类,因此没有“Media Thumbnail”类可以为您解决问题。

    # This is insecure, and only here for a proof of concept.  Your mileage may vary.  Et cetra.
    import PyRSS2Gen
    import datetime
    
    class NoOutput:
        def __init__(self):
            pass
        def publish(self, handler):
            pass
    
    class IPhoneRSS2(PyRSS2Gen.RSSItem):
        def __init__(self, **kwargs):
            PyRSS2Gen.RSSItem.__init__(self, **kwargs)
    
        def publish(self, handler):
            self.do_not_autooutput_description = self.description
            self.description = NoOutput() # This disables the Py2GenRSS "Automatic" output of the description, which would be escaped.
            PyRSS2Gen.RSSItem.publish(self, handler)
    
        def publish_extensions(self, handler):
            handler._out.write('<%s>< ![CDATA[%s]]></%s>' % ("description", self.do_not_autooutput_description, "description"))
    
    # How to use:
    
    rss = PyRSS2Gen.RSS2(
        title = "Example Title",
        link="http://example.com",
        description="Example RSS Output",
        lastBuildDate=datetime.datetime.utcnow(),
        pubDate=datetime.datetime.utcnow(),
        items=[
            IPhoneRSS2(
            title="Item Title",
            description="""<p><b>example</b>text<p><br/>
    <p>Where are you going today?</p>
    
    """,
            link="http://example.com",
            guid="random_guid_x9129319",
            pubDate=datetime.datetime.now()),
        ]
    )
    rss.rss_attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
    rss.write_xml(open("test.xml", "w"), "utf-8")
    

    您提到要在 Feed 中添加图片;您是在描述标签中包含图像的 HTML,还是在其他地方?如果它在其他地方,您能否提供一个示例 RSS 提要,以便我可以根据您的情况进行适当的更改?

    【讨论】:

      【解决方案3】:

      jbm 的回答很好。只是一个加法:Python2.7.5改变了sax库,所以我们需要修改jbm的代码:

      def publish_extensions(self, handler):
          handler._write('<%s><![CDATA[%s]]></%s>' % ("description", self.do_not_autooutput_description, "description"))
      

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 1970-01-01
        • 2015-07-15
        • 2016-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多