我正在使用PyRSS2Gen,我想在我的Feed中为每个项目发布原始HTML(特别是几张图片)。
然而,looking at the source似乎RSSItem的构造函数不接受'image'并且所有HTML都是自动转义的 - 有什么聪明的方法可以解决这个问题吗?
我找到this post,但代码示例似乎不起作用。
如果有人有更好的解决方案,我不会加入PyRSS2Gen。也许我应该写自己的RSS源?
谢谢!
答案 0 :(得分:3)
我从痛苦的经历中了解到,PyRSS2Gen不适合这样做。问题是PyRSS2Gen使用python的sax库,特别是saxutility.xmlwriter,它可以转义所有需要在XML中转义的字符,包括尖括号。因此,即使您扩展PyRSS2Gen以添加标记,它仍然会有问题。
通常情况下,我在RSS(包括XML,而不是html)中看到了包含在CDATA部分中的html。 Python的sax库没有CDATA的概念,但minidom却没有。所以我做的是删除PyRSS2Gen,添加一些我自己代码的额外行,并使用minidom生成XML。
您只需要来自minidom的文档(来自xml.dom.minidom导入文档)
您构建的文档如下:
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()
答案 1 :(得分:2)
我是撰写您列出的博客文章的人。我复制了gist中的代码,并在安装PyRSSGen2后在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>
为了后人的缘故,我将尝试解释下面代码是如何工作的。
就像维也纳上面说的那样,PyRSS2Gen使用内置的SAX库,它自动转义HTML。但是,有办法解决这个问题。在你提到的代码片段中,我覆盖了PyRSS2Gen的“RSSItem”,这样当它输出“description”时,它实际上不会输出任何内容。 (这是包含“NoOutput”类背后的原因)。
由于没有输出描述,我们必须添加一个方法将它自己附加到输出。因此,“publish_extensions”代码(输出media_thumbnail和description标签)。
我可以看到它有些令人困惑(因为你不需要一个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源,以便我可以针对您的情况进行适当的更改?
答案 2 :(得分:0)
jbm的答案很好。只是一个补充:Python2.7.5改变了sax库,所以我们需要修改jbm的代码:
def publish_extensions(self, handler):
handler._write('<%s><![CDATA[%s]]></%s>' % ("description", self.do_not_autooutput_description, "description"))