Python解析RSS(feedparser安装与使用)

RSS

RSS 是用于分发 Web 站点上的内容的摘要的一种简单的 XML 格式。它能够用于共享各种各样的信息。关于RSS的详细信息在(http://www.rssboard.org/rss-profile ),这里面详细的讲了各种值的含义(虽然各个版本的支持不太一样,但是还是比较有通用性的)

RSS(Really Simple Syndication,简易信息聚合)是一种描述和同步网站内容的格式你可以认为是一种定制个性化推送信息的服务。它能够解决你漫无目的的浏览网页的问题。它不会过时,信息越是过剩,它的意义也越加彰显。网络中充斥着大量的信息垃圾,每天摄入了太多自己根本不关心的信息。让自己关注的信息主动来找自己,且这些信息都是用户自己所需要的,这就是RSS的意义。
在这里插入图片描述
我的博客RSS:https://blog.csdn.net/qq_34243930/rss/list
在这里插入图片描述
这里给出博客RSS的源码(部分):


<?xml version="1.0" encoding="utf-8" ?><?xml-stylesheet type="text/xsl" title="XSL Formatting" href="/static_files/rss/rss.xsl" media="all" ?>
<rss version="2.0">
    <channel>
        <title>Ssummer</title>
        <image>
            <link>https://blog.csdn.net/</link>
            <url>https://csdnimg.cn/release/phoenix/static_blog/images/logo.gif</url>
        </image>
        <description>一个小可爱</description>
        <link>https://blog.csdn.net/qq_34243930</link>
        <language>zh-cn</language>
        <generator>https://blog.csdn.net/</generator>
        <ttl>5</ttl>
        <copyright><![CDATA[Copyright &copy; qq_34243930]]></copyright>
        <pubDate>2019/02/08 23:54:44</pubDate>
                    <item>
                                <title><![CDATA[[原]图像处理-图片找不同Find the differences of the two images #Matlab下调用Python接口#SIFT算法应用]]></title>
                <link>https://blog.csdn.net/qq_34243930/article/details/85316240</link>
                <guid>https://blog.csdn.net/qq_34243930/article/details/85316240</guid>
                <author>qq_34243930</author>
                <pubDate>2018/12/28 00:36:22</pubDate>
                <description>
                    <![CDATA[
                    说明
要求(含待处理图片等)、源代码见CSDN下载:
PPT展示、英文报告 见CSDN下载:
大作业要求

Task: Find the differences of the two images, ignoring butterfly
The code should be suitable for all image pairs.
找出经旋转或裁剪之后,像素点并不对应的两幅图片的不同之处。其中不...                    <div>
                        作者:qq_34243930 发表于 2018/12/28 00:36:22 <a href="https://blog.csdn.net/qq_34243930/article/details/85316240">原文链接</a> https://blog.csdn.net/qq_34243930/article/details/85316240                    </div>
                    <div>
                        阅读:38                     </div>
                    ]]>
                </description>
                <category></category>
            </item>

1、generator:这个是生成这个订阅源的工具,也就是博客园自己提供的生成器

2、entry:每篇博文的块,所有的关于你某篇博客的内容都在这(有若干个块,为了不太长我只保留了一个)

3、content:这个里面貌似是可以加html的吗的,rss阅读器展示的就是这的内容

feedparser

feedparser 号称是一个 universal feed parser。
feedparser是一个Python的Feed解析库,可以处理RSS ,CDF,Atom 。使用它我们可从任何 RSS 或 Atom 订阅源得到标题、链接和文章的条目了。

一、feedparser的安装

1、下载feedparser安装包,解压文件夹到自己想放的地方。我使用的是5.2.1
https://pypi.org/project/feedparser/
已上传至CSDN:https://download.csdn.net/download/qq_34243930/10951032

2、修改setup.py文件
用python打开文件夹里面的setup.py文件,修改第一行代码,将from setuptools import setup改为from distutils.core import setup,然后保存。

3、cmd命令行打开,进入setup.py所在目录,执行命令:
python setup.py install

4、测试,关闭pycharm,再重新打开,命令行输入:import feedparser
不报错即为安装成功

这个大家常用的方法我并没有成功
报错如下:
在这里插入图片描述
找到了另一种方法:
如果用你用的是anaconde,方便了太多。
安装时在Anaconda Prompt 下输入:conda install feedparser 即可
在这里插入图片描述
在这里插入图片描述
此时使用import feedparser不会报错:
在这里插入图片描述

二、feedparser的使用

feedparser 最为核心的函数自然是 parse() 解析 URL 地址的函数feedparser.parse()
它返回的究竟是什么样的内容:

{'bozo': 1,
 'bozo_exception': xml.sax._exceptions.SAXParseException('no element found'),
 'encoding': 'utf-8',
 'entries': [],
 'feed': {},
 'namespaces': {},
 'version': ''}

可以看到,得到是一个字典,’feed’(key) 对应的值(value)也是一个字典,’entries’ 则是 list。

在这里插入图片描述
在这里插入图片描述截图放不下这里粘贴一个字典举例:

{'feed': {'title': 'Ssummer',
  'title_detail': {'type': 'text/plain',
   'language': None,
   'base': 'https://blog.csdn.net/qq_34243930/rss/list',
   'value': 'Ssummer'},
  'image': {'links': [{'rel': 'alternate',
     'type': 'text/html',
     'href': 'https://blog.csdn.net/'}],
   'link': 'https://blog.csdn.net/',
   'href': 'https://csdnimg.cn/release/phoenix/static_blog/images/logo.gif'},
  'subtitle': '一个小可爱',
  'subtitle_detail': {'type': 'text/html',
   'language': None,
   'base': 'https://blog.csdn.net/qq_34243930/rss/list',
   'value': '一个小可爱'},
  'links': [{'rel': 'alternate',
    'type': 'text/html',
    'href': 'https://blog.csdn.net/qq_34243930'}],
  'link': 'https://blog.csdn.net/qq_34243930',
  'language': 'zh-cn',
  'generator_detail': {'name': 'https://blog.csdn.net/'},
  'generator': 'https://blog.csdn.net/',
  'ttl': '5',
  'rights': 'Copyright &copy; qq_34243930',
  'rights_detail': {'type': 'text/html',
   'language': None,
   'base': 'https://blog.csdn.net/qq_34243930/rss/list',
   'value': 'Copyright &copy; qq_34243930'},
  'published': '2019/02/09 00:57:35',
  'published_parsed': time.struct_time(tm_year=2019, tm_mon=2, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=32, tm_isdst=0)},
 'entries': [{'title': '[原]图像处理-图片找不同Find the differences of the two images #Matlab下调用Python接口#SIFT算法应用',
   'title_detail': {'type': 'text/plain',
    'language': None,
    'base': 'https://blog.csdn.net/qq_34243930/rss/list',
    'value': '[原]图像处理-图片找不同Find the differences of the two images #Matlab下调用Python接口#SIFT算法应用'},
   'links': [{'rel': 'alternate',
     'type': 'text/html',
     'href': 'https://blog.csdn.net/qq_34243930/article/details/85316240'}],
   'link': 'https://blog.csdn.net/qq_34243930/article/details/85316240',
   'id': 'https://blog.csdn.net/qq_34243930/article/details/85316240',
   'guidislink': False,
   'authors': [{'name': 'qq_34243930'}],
   'author': 'qq_34243930',
   'author_detail': {'name': 'qq_34243930'},
   'published': '2018/12/28 00:36:22',
   'published_parsed': time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=32, tm_isdst=0),
   'summary': '说明\n要求(含待处理图片等)、源代码见CSDN下载:\nPPT展示、英文报告 见CSDN下载:\n大作业要求\n\nTask: Find the differences of the two images, ignoring butterfly\nThe code should be suitable for all image pairs.\n找出经旋转或裁剪之后,像素点并不对应的两幅图片的不同之处。其中不...                    <div>\n                        作者:qq_34243930 发表于 2018/12/28 00:36:22 <a href="https://blog.csdn.net/qq_34243930/article/details/85316240">原文链接</a> https://blog.csdn.net/qq_34243930/article/details/85316240                    </div>\n                    <div>\n                        阅读:38                     </div>',
   'summary_detail': {'type': 'text/html',
    'language': None,
    'base': 'https://blog.csdn.net/qq_34243930/rss/list',
    'value': '说明\n要求(含待处理图片等)、源代码见CSDN下载:\nPPT展示、英文报告 见CSDN下载:\n大作业要求\n\nTask: Find the differences of the two images, ignoring butterfly\nThe code should be suitable for all image pairs.\n找出经旋转或裁剪之后,像素点并不对应的两幅图片的不同之处。其中不...                    <div>\n                        作者:qq_34243930 发表于 2018/12/28 00:36:22 <a href="https://blog.csdn.net/qq_34243930/article/details/85316240">原文链接</a> https://blog.csdn.net/qq_34243930/article/details/85316240                    </div>\n                    <div>\n                        阅读:38                     </div>'},
   'tags': []},
   
    #  这里还有很多个feed{title:....},{},{},
    
 'bozo': 0,
 'headers': {'Server': 'openresty',
  'Date': 'Fri, 08 Feb 2019 16:57:35 GMT',
  'Content-Type': 'text/xml; charset=UTF-8',
  'Transfer-Encoding': 'chunked',
  'Connection': 'close',
  'Vary': 'Accept-Encoding',
  'Set-Cookie': 'uuid_tt_dd=10_18804008730-1549645055782-268903; Expires=Thu, 01 Jan 2025 00:00:00 GMT; Path=/; Domain=.csdn.net;',
  'Strict-Transport-Security': 'max-age= 31536000',
  'Content-Encoding': 'gzip'},
 'href': 'https://blog.csdn.net/qq_34243930/rss/list',
 'status': 200,
 'encoding': 'UTF-8',
 'version': 'rss20',
 'namespaces': {}}

基本用法

每个 RSS 和 Atom 订阅源都包含一个标题(d.feed.title)和一组文章条目(d.entries)。
通常,每个文章条目都有一段摘要(d.entries[i].summary),或者是包含了条目中实际文本的描述性标签(d.entries[i].description)。

import feedparser
d=feedparser.parse('https://blog.csdn.net/qq_34243930/rss/list')

在这里插入图片描述

  1. d.feed
    feed 对应一个字典
import feedparser
d=feedparser.parse('https://blog.csdn.net/qq_34243930/rss/list')

d['feed']['title']
Out[9]: 'Ssummer'

d.feed.title     # 通过属性的方式访问 
Out[10]: 'Ssummer'

d.feed.link
Out[11]: 'https://blog.csdn.net/qq_34243930'

d.feed.subtitle
Out[12]: '一个小可爱'

在这里插入图片描述

  1. d.entries
type(d.entries)
Out[18]: list

len(d.entries)
Out[19]: 20     # 共20篇文章

[e.title for e in d.entries][:3]
Out[20]: 
['[原]图像处理-图片找不同Find the differences of the two images #Matlab下调用Python接口#SIFT算法应用',
 '[原]《斯坦福CS224n NLP》-Week1#',
 '[原]《斯坦福CS224n NLP》-Week2#']
 
d.entries[0].summary      # 第一篇文章的摘要信息
Out[21]: '说明\n要求(含待处理图片等)、源代码见CSDN下载:\nPPT展示、英文报告 见CSDN下载:\n大作业要求\n\nTask: Find the differences of the two images, ignoring butterfly\nThe code should be suitable for all image pairs.\n找出经旋转或裁剪之后,像素点并不对应的两幅图片的不同之处。其中不...                    <div>\n                        作者:qq_34243930 发表于 2018/12/28 00:36:22 <a href="https://blog.csdn.net/qq_34243930/article/details/85316240">原文链接</a> https://blog.csdn.net/qq_34243930/article/details/85316240                    </div>\n                    <div>\n                        阅读:38                     </div>'

d.entries[0].summary == d.entries[0].description
Out[22]: True
# 有些条目可能会不提供 summary 信息
# 此时 summary 信息可通过 description 获得


在这里插入图片描述

更多使用方法参考:

Python 模块feedparser安装使用:
https://blog.csdn.net/DarrenXf/article/details/82669506
python feedparser 使用:
http://www.cnblogs.com/youxin/archive/2013/06/12/3132713.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏普通

谢谢打赏~普通在此谢过

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值