当前位置: 首页 > news >正文

Python的RSS/Atom源解析库feedparser

文章目录

  • 简介
  • 安装
  • 初试
  • RSS
    • 访问公共元素
    • 访问每条元素
    • 访问图片
    • 访问附件
    • 访问云
  • Atom
    • 访问公共元素
    • 访问每条元素
    • 访问贡献者
    • 访问多个链接
  • 高级功能
  • HTTP功能
  • 示例
  • 参考文献

简介

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

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

特性AtomRSS
全称Atom Syndication FormatReally 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可能会改变。




安装

pip install feedparser

Python 3.8+ 可用




初试

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




RSS

访问公共元素

import feedparserd = 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)



访问每条元素

import feedparserd = 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)



访问图片

import feedparserd = 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}



访问附件

import feedparserd = 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'}



访问云

import feedparserd = 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

访问公共元素

import feedparserd = 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)



访问每条元素

import feedparserd = 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>'}]



访问贡献者

import feedparserd = 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'}]



访问多个链接

import feedparserd = 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

文章转载自:

http://oO6MDAkb.cnhgc.cn
http://PEaDQQNE.cnhgc.cn
http://fMPNcRld.cnhgc.cn
http://8MMWY6FY.cnhgc.cn
http://YYJQ6s0j.cnhgc.cn
http://iAGf07jU.cnhgc.cn
http://Y0fJIaa9.cnhgc.cn
http://o2YCIlfY.cnhgc.cn
http://EXzv17xB.cnhgc.cn
http://no7Zt4MP.cnhgc.cn
http://yfFQPbua.cnhgc.cn
http://gKVSompN.cnhgc.cn
http://E4PbJLlL.cnhgc.cn
http://45VjVo4M.cnhgc.cn
http://EfvjdokU.cnhgc.cn
http://HEnDjaba.cnhgc.cn
http://EIjHlMQQ.cnhgc.cn
http://QvOhjXGo.cnhgc.cn
http://krCpad75.cnhgc.cn
http://ouBFABHq.cnhgc.cn
http://gpliutZw.cnhgc.cn
http://JWwSRYTi.cnhgc.cn
http://IqCuomb4.cnhgc.cn
http://rPHQC4uI.cnhgc.cn
http://AuE7edJg.cnhgc.cn
http://TJxLjB6O.cnhgc.cn
http://2Krh0D9N.cnhgc.cn
http://vSF6HKh8.cnhgc.cn
http://DiK64tzP.cnhgc.cn
http://3n0W61Sr.cnhgc.cn
http://www.dtcms.com/a/368777.html

相关文章:

  • 抓虫:loongarch64架构selinux强防开启程序执行报错execmod
  • 酷柚易汛ERP 2025-09-05系统升级日志
  • STM32——WDG看门狗
  • Redis 发布订阅:社区的 “通知栏与分类订阅” 系统
  • WordPress性能优化全攻略:从插件实战到系统级优化
  • [新启航]激光频率梳 3D 轮廓测量 - 蓝光机械 3D 扫描的工作原理及优缺点
  • 3DEXPERIENCE平台五大实用技巧指南
  • 彻底搞懂深度学习-模型压缩(减枝、量化、知识蒸馏)
  • 概率论第二讲——一维随机变量及其分布
  • ChartGPT深度体验:AI图表生成工具如何高效实现数据可视化与图表美化?
  • 【AndroidStudio】官网下载免安装版,AndroidStudio压缩版的配置和使用
  • Android Activity的启动流程
  • 将 Android 设备的所有系统日志(包括内核日志、系统服务日志等)完整拷贝到 Windows 本地
  • NGUI--三大基础控件
  • 服务器IP暴露被攻击了怎么办?
  • Transformer实战——使用 run_glue.py 微调模型
  • SQLalachemy 错误 - Lost connection to MySQL server during query
  • 门控MLP(Qwen3MLP)与稀疏混合专家(Qwen3MoeSparseMoeBlock)模块解析
  • React Hooks useContext
  • 【Linux】Linux 的 cp -a 命令的作用
  • 基于FPGA实现CRC校验码算法(以MODBUS中校验码要求为例)verilog代码+仿真验证
  • LeetCode刷题-top100( 矩阵置零)
  • 算法模板(Java版)_DFS与BFS
  • 一分钟了解Modbus 转 IEC61850 网关
  • Webpack 有哪些特性?构建速度?如何优化?
  • 2025精选5款AI视频转文字工具,高效转录秒变文字!
  • 【最新版】发烧级完美解码播放器PureCodec v2025.08.29 中文免费版_电脑播放器影音解码包
  • 阿里云国际代理:阿里云的云数据库是什么?
  • 盲盒抽卡机小程序功能版块设计的合理性评估维度
  • Memory write error at 0x100000. MMU page translation fault