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. <>
相关推荐
wefly201716 小时前
jsontop.cn:一站式 JSON 全能工具集,开发全流程效率神器
前端·javascript·python·django·json·json在线转换
ALex_zry17 小时前
C++ ORM与数据库访问层设计:Repository模式实战
开发语言·数据库·c++
li99yo19 小时前
3DGS的复现
图像处理·pytorch·经验分享·python·3d·conda·pip
潜创微科技--高清音视频芯片方案开发1 天前
2026年C转DP芯片方案深度分析:从适配场景到成本性能的优选指南
c语言·开发语言
Dontla1 天前
用pip install -e .开发Python包时,Python项目目录结构(项目结构)(可编辑安装editable install)
python·pip
Thomas.Sir1 天前
第三章:Python3 之 字符串
开发语言·python·字符串·string
刘景贤1 天前
C/C++开发环境
开发语言·c++
威联通网络存储1 天前
告别掉帧与素材损毁:威联通 QuTS hero 如何重塑影视后期协同工作流
前端·网络·人工智能·python