如何使用feedparser解析"<media:group>"?

5 投票
2 回答
3060 浏览
提问于 2025-04-15 20:31

下面是一个rss文件的内容,我想获取media:group部分的内容。我查看了feedparser的文档,但似乎没有提到这个部分。请问该怎么做呢?任何帮助都非常感谢。

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:ymusic="http://music.yahoo.com/rss/1.0/ymusic/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel>
        <title>XYZ InfoX:  Special hello  </title>
        <link>http://www1.XYZInfoX.com/learninghello/home</link>
        <description>hello</description>
        <language>en</language>         <copyright />
        <pubDate>Wed, 17 Mar 2010 08:50:06 GMT</pubDate>
        <dc:creator />
        <dc:date>2010-03-17T08:50:06Z</dc:date>
        <dc:language>en</dc:language> <dc:rights />
        <image>
            <title>Voice of America</title>
            <link>http://www1.XYZInfoX.com/learninghello</link>
            <url>http://media.XYZInfoX.com/designimages/XYZRSSIcon.gif</url>
        </image>

        <item>
                <title>Who Were the Deadliest Gunmen of the Wild West?</title>
                <link>http://www1.XYZInfoX.com/learninghello/home/Deadliest-Gunmen-of-the-Wild-West-87826807.html</link>
                <description> The story of two of them: "Killin'" Jim Miller was an outlaw, "Texas" John Slaughter was a lawman | EXPLORATIONS  </description>
                <pubDate>Wed, 17 Mar 2010 00:38:48 GMT</pubDate>
                <guid isPermaLink="false">87826807</guid>
                <dc:creator></dc:creator>
                <dc:date>2010-03-17T00:38:48Z</dc:date>                                                                                                                                     
                <media:group>
                    <media:content url="http://media.XYZInfoX.com/images/archives_peace_comm_480_16mar_se.jpg" medium="image" isDefault="true" height="300" width="480" />
                    <media:content url="http://media.XYZInfoX.com/images/archives_peace_comm_230_16mar_se_edited-1.jpg" medium="image" isDefault="false" height="230" width="230" />
                    <media:content url="http://media.XYZInfoX.com/images/tex_trans_lawmans_230_16mar10_se.jpg" medium="image" isDefault="false" height="230" width="230" />
                    <media:content url="http://www.XYZInfoX.com/MediaAssets2/learninghello/dalet/se-exp-outlaws-part2-17mar2010.Mp3" type="audio/mpeg" medium="audio" isDefault="false" />
                </media:group>
     </item>

2 个回答

-1

你可以使用

feed = feedparser.parse(your_feeds_url)

来解析这个数据源,然后可以通过两种方式来访问你的xml元素:一种是用python的属性访问,另一种是像访问字典一样访问feed及其子元素。前一种方法在处理像media:content这样的元素名时可能会出问题,所以建议使用后一种方法。

其余的内容在查看http://www.feedparser.org上的示例后应该会变得更清晰。

7

从PyPi上获取的feedparser 4.1版本有个问题。

对我来说,解决办法是从代码库里下载最新的feedparser.py(4.2预发布版)。

svn checkout http://feedparser.googlecode.com/svn/trunk/ feedparser-readonly
cd feedparser-readonly
python setup.py install

现在你可以访问所有的mrss项目了。

>>> import feedparser  # the new version!
>>> d = feedparser.parse(MY_XML_URL)
>>> for content in d.entries[0].media_content: print content['url']

这样就能解决你的问题了。

撰写回答