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. <>
相关推荐
立志成为大牛的小牛1 小时前
数据结构——二十六、邻接表(王道408)
开发语言·数据结构·c++·学习·程序人生
祈祷苍天赐我java之术1 小时前
Redis 数据类型与使用场景
java·开发语言·前端·redis·分布式·spring·bootstrap
Gitpchy1 小时前
Day 20 奇异值SVD分解
python·机器学习
MediaTea2 小时前
Python 第三方库:matplotlib(科学绘图与数据可视化)
开发语言·python·信息可视化·matplotlib
草莓熊Lotso2 小时前
C++ 方向 Web 自动化测试入门指南:从概念到 Selenium 实战
前端·c++·python·selenium
JS.Huang2 小时前
【JavaScript】原生函数
开发语言·javascript·ecmascript
我是李武涯2 小时前
PyTorch Dataloader工作原理 之 default collate_fn操作
pytorch·python·深度学习
CoderCodingNo3 小时前
【GESP】C++五级考试大纲知识点梳理, (5) 算法复杂度估算(多项式、对数)
开发语言·c++·算法
Kratzdisteln3 小时前
【Python】绘制椭圆眼睛跟随鼠标交互算法配图详解
python·数学·numpy·pillow·matplotlib·仿射变换
maxruan3 小时前
PyTorch学习
人工智能·pytorch·python·学习