Python的RSS/Atom源解析库feedparser

文章目录

简介

feedparser 是 Python 的 RSS/Atom 源解析库,它能够将复杂的 XML 订阅源转换为易于操作的 Python 对象。

Atom 源是一种 XML 格式的文件,用于发布经常更新的内容(如博客文章、新闻头条、音频、视频等),允许用户通过订阅来获取网站的最新内容更新。​​它和更早的 ​​RSS 源在功能上几乎完全相同,可以理解为同一件事物的两个不同版本。Atom 是 RSS 的改进版本,解决了一些RSS 早期存在的模糊和不一致问题

特性 Atom RSS
全称 Atom Syndication Format Really Simple Syndication(或 Rich Site Summary)
诞生时间 2005年 1999年
规范性 高度规范,由IETF标准化(RFC 4287),定义严格,歧义少。 较为松散,有多个不同版本(0.90, 0.91, 1.0, 2.0),存在兼容性问题。
扩展性 通过XML命名空间更好地支持扩展,可以嵌入其他格式的内容(如XHTML)。 扩展性相对较弱。
内容包含 明确区分了摘要(<summary>)和内容(<content>)。 定义相对模糊。
唯一标识符 强制要求 每个条目必须有全球唯一的永久ID(<id>),这对于跟踪内容非常可靠。 不是强制要求,通常使用URL作为标识,但URL可能会改变。

安装

shell 复制代码
pip install feedparser

Python 3.8+ 可用

初试

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/atom10.xml')
print(d)
print(d['feed'])
print(d['feed']['title'])  # Sample Feed

RSS

访问公共元素

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/rss20.xml')
print(d.feed.title)  # Sample Feed
print(d.feed.link)  # http://example.org/
print(d.feed.description)  # For documentation <em>only</em>
print(d.feed.published)  # Sat, 07 Sep 2002 00:00:01 GMT
print(d.feed.published_parsed)  # time.struct_time(tm_year=2002, tm_mon=9, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=5, tm_yday=250, tm_isdst=0)

访问每条元素

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/rss20.xml')
print(d.entries[0].id)  # http://example.org/guid/1
print(d.entries[0].title)  # First item title
print(d.entries[0].link)  # http://example.org/item/1
print(d.entries[0].description)  # Watch out for <span>nasty tricks</span>
print(d.entries[0].published)  # Thu, 05 Sep 2002 00:00:01 GMT
print(d.entries[0].published_parsed)  # time.struct_time(tm_year=2002, tm_mon=9, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=248, tm_isdst=0)

访问图片

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/rss20.xml')
print(d.feed.image)
# {'href': 'http://example.org/banner.png', 'title': 'Example banner', 'title_detail': {'type': 'text/plain', 'language': None, 'base': 'https://feedparser.readthedocs.io/en/latest/examples/rss20.xml', 'value': 'Example banner'}, 'links': [{'rel': 'alternate', 'type': 'text/html', 'href': 'http://example.org/'}], 'link': 'http://example.org/', 'width': 80, 'height': 15}

访问附件

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/rss20.xml')
e = d.entries[0]
print(len(e.enclosures))  # 1
print(e.enclosures[0])  # {'length': '1069871', 'type': 'audio/mpeg', 'href': 'http://example.org/audio/demo.mp3'}

访问云

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/rss20.xml')
print(d.feed.cloud)
# {'domain': 'rpc.example.com', 'port': '80', 'path': '/RPC2', 'registerprocedure': 'pingMe', 'protocol': 'soap'}

Atom

访问公共元素

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/atom10.xml')
print(d.feed.id)  # tag:feedparser.org,2005-11-09:/docs/examples/atom10.xml
print(d.feed.title)  # Sample Feed
print(d.feed.link)  # http://example.org/
print(d.feed.subtitle)  # For documentation <em>only</em>
print(d.feed.updated)  # 2005-11-09T11:56:34Z
print(d.feed.updated_parsed)  # time.struct_time(tm_year=2005, tm_mon=11, tm_mday=9, tm_hour=11, tm_min=56, tm_sec=34, tm_wday=2, tm_yday=313, tm_isdst=0)

访问每条元素

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/atom10.xml')
print(d.entries[0].id)  # tag:feedparser.org,2005-11-09:/docs/examples/atom10.xml:3
print(d.entries[0].title)  # First entry title
print(d.entries[0].link)  # http://example.org/entry/3
print(d.entries[0].published)  # 2005-11-09T00:23:47Z
print(d.entries[0].published_parsed)  # time.struct_time(tm_year=2005, tm_mon=11, tm_mday=9, tm_hour=0, tm_min=23, tm_sec=47, tm_wday=2, tm_yday=313, tm_isdst=0)
print(d.entries[0].updated)  # 2005-11-09T11:56:34Z
print(d.entries[0].updated_parsed)  # time.struct_time(tm_year=2005, tm_mon=11, tm_mday=9, tm_hour=11, tm_min=56, tm_sec=34, tm_wday=2, tm_yday=313, tm_isdst=0)
print(d.entries[0].summary)  # Watch out for nasty tricks
print(d.entries[0].content)  # [{'type': 'application/xhtml+xml', 'language': 'en-US', 'base': 'http://example.org/entry/3', 'value': 'Watch out for <span> nasty tricks</span>'}]

访问贡献者

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/atom10.xml')
e = d.entries[0]
print(len(e.contributors))  # 2
print(e.contributors)
# [{'name': 'Joe', 'href': 'http://example.org/joe/', 'email': 'joe@example.org'}, {'name': 'Sam', 'href': 'http://example.org/sam/', 'email': 'sam@example.org'}]

访问多个链接

python 复制代码
import feedparser

d = feedparser.parse('https://feedparser.readthedocs.io/en/latest/examples/atom10.xml')
e = d.entries[0]
print(len(e.links))  # 4
print(e.links)
# [{'rel': 'alternate', 'href': 'http://example.org/entry/3', 'type': 'text/html'}, {'rel': 'related', 'type': 'text/html', 'href': 'http://search.example.com/'}, {'rel': 'via', 'type': 'text/html', 'href': 'http://toby.example.com/examples/atom10'}, {'rel': 'enclosure', 'type': 'video/mpeg4', 'href': 'http://www.example.com/movie.mp4', 'length': '42301'}]

高级功能

HTTP功能

示例

参考文献

  1. GitHub - kurtmckee/feedparser: Parse feeds in Python
  2. Documentation --- feedparser
  3. <>
  4. <>
  5. <>
  6. <>
  7. <>
  8. <>
  9. <>
  10. <>
相关推荐
PixelMind5 分钟前
【LLIE技术专题】 SCI代码讲解
图像处理·python·低照度图像增强·llie
花心蝴蝶.8 分钟前
Java 中的代理模式
java·开发语言·代理模式
风语者66616 分钟前
perl踩坑系列=====正则表达式捕获
开发语言·perl
我科绝伦(Huanhuan Zhou)19 分钟前
银河麒麟V10编译perl-5.42.0,并设置环境变量
开发语言·perl
大飞pkz24 分钟前
【设计模式】享元模式
开发语言·设计模式·c#·享元模式
天才测试猿27 分钟前
Python常用自动化测试框架—Pytest详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
茉莉玫瑰花茶38 分钟前
C++扩展 --- 并发支持库(补充3)
开发语言·c++
胡耀超40 分钟前
2、CPU深度解析:从微架构到性能优化
python·性能优化·架构·arm·cpu·x86·多核心
一只乔哇噻1 小时前
java后端工程师进修ing(研一版‖day49)
java·开发语言
枫叶丹41 小时前
【Qt开发】输入类控件(二)-> QTextEdit
开发语言·qt