python解析RSS(feedparser)

mrbean

发表于2014-05-18 17:07:00

虽然说当今的博客已经不像前几年那么火了,但是RSS还是一项很有创造性和实用性的东西。RSS 是用于分发 Web 站点上的内容的摘要的一种简单的 XML 格式。它能够用于共享各种各样的信息。关于RSS的详细信息在(http://www.rssboard.org/rss-profile),这里面详细的讲了各种值的含义(虽然各个版本的支持不太一样,但是还是比较有通用性的)。这里我先介绍一下怎么使用feedparser操作RSS然后再介绍一些常用的属性,也方便大家实验:

一、feedparser的安装
首先就是下载源码编译安装,另外用pip、easy-install也都可以安装

二、feedparser的使用
不得不说feedparser非常的强大不仅是因为它对各种rss版本的兼容性非常好,而且使用起来很简单,就一个函数(如假包换)

上代码:

d = feedparser.parse('http://feed.cnblogs.com/blog/u/161528/rss')

可以看到就只有一个parse函数,这里传入的是我的博客的rss地址,这时候rss的全部内容就都在这个d里面了
以博客园的rss为例介绍一些值的含义(个人理解可能会有错误欢迎指正)

首先给出简化的我的博客的rss

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title type="text">博客园_mrbean</title>
    <subtitle type="text">**********************</subtitle>
    <id>uuid:32303acf-fb5f-4538-a6ba-7a1ac4fd7a58;id=8434</id>
    <updated>2014-05-14T15:13:36Z</updated>
    <author>
        <name>mrbean</name>
        <uri>http://www.cnblogs.com/MrLJC/</uri>
    </author>
    <generator>feed.cnblogs.com</generator>
    <entry>
        <id>http://www.cnblogs.com/MrLJC/p/3715783.html</id>
        <title type="text">用python读写excel(xlrd、xlwt) - mrbean</title>
        <summary type="text">最近需要从多个excel表里面用各种方式整...</summary>
        <published>2014-05-08T16:25:00Z</published>
        <updated>2014-05-08T16:25:00Z</updated>
        <author>
            <name>mrbean</name>
            <uri>http://www.cnblogs.com/MrLJC/</uri>
        </author>
        <link rel="alternate" href="http://www.cnblogs.com/MrLJC/p/3715783.html" />
        <link rel="alternate" type="text/html" href="http://www.cnblogs.com/MrLJC/p/3715783.html" />
        <content type="html">最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在学python,所以当然就决定用python尝试一下了。发现python果然简洁很多。这里简单记录一下。(由于是用到什么学什么,所以不算太深入,高手勿喷,欢迎指导)一、读excel表读excel要用...&lt;img src="http://counter.cnblogs.com/blog/rss/3715783" width="1" height="1" alt=""/&gt;&lt;br/&gt;&lt;p&gt;本文链接:&lt;a href="http://www.cnblogs.com/MrLJC/p/3715783.html" target="_blank"&gt;用python读写excel(xlrd、xlwt)&lt;/a&gt;,转载请注明。&lt;/p&gt;</content>
    </entry>
</feed>

这个里面的值就是我们得到的内容大部分都是见名知意的单词,当然这些很多都是博客园的生成器自己家的。简单说几个不太明白的

1、generator:这个是生成这个订阅源的工具,也就是博客园自己提供的生成器
2、entry:每篇博文的块,所有的关于你某篇博客的内容都在这(有若干个块,为了不太长我只保留了一个)
3、content:这个里面貌似是可以加html的吗的,rss阅读器展示的就是这的内容

下面的代码演示了如何查询这些内容:

>>> import feedparser
>>> feed=feedparser.parse("test.xml")
>>> print feed.feed.title
博客园_mrbean
>>> print feed['feed']['title']
博客园_mrbean
>>> print feed.entries[0].id    #这个要注意
http://www.cnblogs.com/MrLJC/p/3715783.html
>>> print feed.entries[0].content
[{'base': u'', 'type': u'text/html', 'value': u'\u6700\u8fd1\u9700\u8981\u4ece\u591a\u4e2aexcel\u8868\u91cc\u9762\u7528\u5404\u79cd\u65b9\u5f0f\u6574\u7406\u4e00\u4e9b\u6570\u636e\uff0c\u867d\u7136\u8bf4\u539f\u6765\u7528\u8fc7java\u505a\u8fd9\u7c7b\u4e8b\u60c5\uff0c\u4f46\u662f\u7531\u4e8e\u6700\u8fd1\u5728\u5b66python\uff0c\u6240\u4ee5\u5f53\u7136\u5c31\u51b3\u5b9a\u7528python\u5c1d\u8bd5\u4e00\u4e0b\u4e86\u3002\u53d1\u73b0python\u679c\u7136\u7b80\u6d01\u5f88\u591a\u3002\u8fd9\u91cc\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u3002\uff08\u7531\u4e8e\u662f\u7528\u5230\u4ec0\u4e48\u5b66\u4ec0\u4e48\uff0c\u6240\u4ee5\u4e0d\u7b97\u592a\u6df1\u5165\uff0c\u9ad8\u624b\u52ff\u55b7\uff0c\u6b22\u8fce\u6307\u5bfc\uff09\u4e00\u3001\u8bfbexcel\u8868\u8bfbexcel\u8981\u7528...<img alt="" height="1" src="http://counter.cnblogs.com/blog/rss/3715783" width="1" /><br /><p>\u672c\u6587\u94fe\u63a5\uff1a<a href="http://www.cnblogs.com/MrLJC/p/3715783.html" target="_blank">\u7528python\u8bfb\u5199excel\uff08xlrd\u3001xlwt\uff09</a>\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u3002</p>', 'language': None}]

 

 作者:mrbean 原文地址:http://www.cnblogs.com/MrLJC/p/3731213.html