Python解析iTunes播客XML Feed需用xml.etree.ElementTree或feedparser:前者需手动声明命名空间映射(如{"itunes": "https://www.php.cn/link/5647ed707e5b7b343c59502684365085"})提取title、enclosure.url、itunes:duration等字段;后者自动归一化为entry.itunes_duration等属性,更省心。

用Python解析iTunes播客的XML Feed,核心是读取标准RSS(含iTunes扩展)格式的XML文档,并提取标题、描述、音频链接、发布日期、封面图等字段。关键在于正确处理命名空间,尤其是 itunes: 前缀的元素。
1. 理解播客Feed的XML结构
iTunes播客Feed本质是符合RSS 2.0规范的XML文件,额外引入了iTunes DTD定义的命名空间:https://www.php.cn/link/5647ed707e5b7b343c59502684365085//EN。常见带前缀的字段包括:
-
<author></author>— 播客作者 -
<summary></summary>— 简短摘要 -
<subtitle></subtitle>— 副标题 -
<image href="..."></image>— 封面图URL -
<duration></duration>— 单集时长(如 "42:18") -
<enclosure url="..." type="audio/mpeg"></enclosure>— 音频附件(实际下载地址)
2. 使用xml.etree.ElementTree解析(推荐)
Python标准库 xml.etree.ElementTree 轻量且足够处理大多数播客Feed。需显式声明命名空间映射,否则无法定位 itunes: 元素。
示例代码:
Python 环境工具专业版功能概述Python 环境工具专业版是一项面向实际任务的技能。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。核心要点使用时应结合输入条件选择合适的执行方式,核对必要参数、依赖环境与输出内容,并按原始要求处理异常情况。该技能适合需要稳定复用相关能力的场景,可作为自动化工作流的一部分,也便于后续检查、调整和扩展。执行完成后建议检查结果是否符合预期,并保留必要记录。使用与执行同时注意输入完整性、执行条件和输出可用性。从功能定位来看,该技能
立即学习“Python免费学习笔记(深入)”;
import xml.etree.ElementTree as ET
import requests
<h1>获取Feed内容(可本地文件或远程URL)</h1><p>url = "<a href="https://www.php.cn/link/4b0e9d0b2ec615f32fc627d50cd04c5f">https://www.php.cn/link/4b0e9d0b2ec615f32fc627d50cd04c5f</a>"
response = requests.get(url)
response.raise_for_status()
root = ET.fromstring(response.content)</p><h1>定义命名空间映射(key为前缀,value为URI)</h1><p>ns = {
"rss": "<a href="https://www.php.cn/link/429be53185d62d6d41561471fdd129a1">https://www.php.cn/link/429be53185d62d6d41561471fdd129a1</a>",
"itunes": "<a href="https://www.php.cn/link/5647ed707e5b7b343c59502684365085">https://www.php.cn/link/5647ed707e5b7b343c59502684365085</a>"
}</p><h1>提取频道信息(channel节点下)</h1><p>channel = root.find("rss:channel", ns)
if channel is not None:
title = channel.find("rss:title", ns).text if channel.find("rss:title", ns) is not None else ""
description = channel.find("rss:description", ns).text or ""
itunes_author = channel.find("itunes:author", ns).text if channel.find("itunes:author", ns) is not None else ""</p><pre class="brush:php;toolbar:false;"># 封面图(注意:是 <itunes:image> 标签的 href 属性)
itunes_image = channel.find("itunes:image", ns)
image_url = itunes_image.get("href") if itunes_image is not None else ""提取所有 episode(item 节点)
episodes = [] for item in channel.findall("rss:item", ns): ep = {} ep["title"] = item.find("rss:title", ns).text or "" ep["pubDate"] = item.find("rss:pubDate", ns).text or ""
# 音频地址来自 enclosure 标签的 url 属性
enclosure = item.find("rss:enclosure", ns)
ep["audio_url"] = enclosure.get("url") if enclosure is not None else ""
# iTunes扩展字段
ep["duration"] = item.find("itunes:duration", ns).text if item.find("itunes:duration", ns) is not None else ""
ep["summary"] = item.find("itunes:summary", ns).text or ""
episodes.append(ep)print(f"共解析 {len(episodes)} 集播客")
3. 处理常见问题
-
命名空间不匹配:不同Feed可能使用略有差异的RSS命名空间(如无前缀、
atom:混用),建议先用ET.tostring(root, encoding='unicode')打印开头几行,确认实际使用的命名空间URI -
编码错误:部分Feed返回非UTF-8编码(如ISO-8859-1),可用
response.content.decode('latin-1')再传给ET.fromstring() -
缺少必要字段:如
<enclosure></enclosure>缺失时,有些Feed改用<link>或<content></content>(需额外注册 media 命名空间) -
日期格式解析:RSS的
<pubdate></pubdate>是RFC 2822格式,可用email.utils.parsedate_to_datetime()转为datetime对象
4. 替代方案:feedparser(更省心)
如果不想手动处理XML和命名空间,feedparser 库专为RSS/Atom设计,自动解析iTunes扩展字段并归一化为字典键(如 entry.itunes_duration, entry.itunes_image)。
安装与使用:
pip install feedparser
<p>import feedparser
d = feedparser.parse("<a href="https://www.php.cn/link/4b0e9d0b2ec615f32fc627d50cd04c5f">https://www.php.cn/link/4b0e9d0b2ec615f32fc627d50cd04c5f</a>")</p><p>print(d.feed.title) # 频道标题
print(d.feed.itunes_author) # 频道作者(自动映射)</p><p>for entry in d.entries:
print(entry.title)
print(entry.enclosures[0].href) # 音频地址
print(entry.itunes_duration)
注意:feedparser会把所有命名空间字段扁平化为 entry.xxx 形式,但字段名可能因Feed而异(如 itunes_summary 或 summary_detail.value),建议打印 dir(entry) 查看可用属性。

















