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. <>
相关推荐
Ray Liang11 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮11 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling11 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮15 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽15 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程2 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python