python - 使用 feedparser 访问重复的 feed 标签

标签 python rss feedparser

我正在尝试解析此提要:https://feeds.podcastmirror.com/dudesanddadspodcast

channel 部分有两个 podcast:person 条目

<podcast:person role="host" img="https://dudesanddadspodcast.com/files/2019/03/andy.jpg" href="https://www.podchaser.com/creators/andy-lehman-107aRuVQLA">Andy Lehman</podcast:person>
<podcast:person role="host" img="https://dudesanddadspodcast.com/files/2019/03/joel.jpg" href="https://www.podchaser.com/creators/joel-demott-107aRuVQLH" >Joel DeMott</podcast:person>

解析时,feedparser 只引入一个名称

> import feedparser
> d = feedparser.parse('https://feeds.podcastmirror.com/dudesanddadspodcast')
> d.feed['podcast_person']
> {'role': 'host', 'img': 'https://dudesanddadspodcast.com/files/2019/03/joel.jpg', 'href': 'https://www.podchaser.com/creators/joel-demott-107aRuVQLH'}

我要更改什么才能显示 podcast_person 列表,以便我可以循环浏览每个列表?

最佳答案

想法#1:

from bs4 import BeautifulSoup
import requests

r = requests.get("https://feeds.podcastmirror.com/dudesanddadspodcast").content
soup = BeautifulSoup(r, 'html.parser')

soup.find_all("podcast:person")

输出:

[<podcast:person href="https://www.podchaser.com/creators/andy-lehman-107aRuVQLA" img="https://dudesanddadspodcast.com/files/2019/03/andy.jpg" role="host">Andy Lehman</podcast:person>,
 <podcast:person href="https://www.podchaser.com/creators/joel-demott-107aRuVQLH" img="https://dudesanddadspodcast.com/files/2019/03/joel.jpg" role="host">Joel DeMott</podcast:person>,
 <podcast:person href="https://www.podchaser.com/creators/cory-martin-107aRwmCuu" img="" role="guest">Cory Martin</podcast:person>,
 <podcast:person href="https://www.podchaser.com/creators/julie-lehman-107aRuVQPL" img="" role="guest">Julie Lehman</podcast:person>]

想法#2:

导入 feedparser

d = feedparser.parse('https://feeds.podcastmirror.com/dudesanddadspodcast')
hosts = d.entries[1]['authors'][1]['name'].split(", ")

print("The hosts of this Podcast are {} and {}.".format(hosts[0], hosts[1]))

输出:

The hosts of this Podcast are Joel DeMott and Andy Lehman.

关于python - 使用 feedparser 访问重复的 feed 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68521843/

相关文章:

python - 将给定的 pandas 数据帧转换为另一个数据帧

python - 如何使用新格式打印大括号

python - 用 python 解析 Facebook feed 日期时间?

python - 从 tf.distributions.Categorical 输出层创建 softmax

python - HTTP 回调 URL 与 WebSocket 的异步响应?

java - Android volley 使用 intentservice 发送数据两次?

Ruby RSS/Atom 处理器,它知道我已经在提要中处理了哪些项目

iphone - 标记内的MWFeedParser标记(媒体:缩略图)

python - Python中使用feedparser的updated_pa​​rsed和published_pa​​rsed的区别

python - 使用 lxml 解析 RSS-Feed 的方法不那么痛苦?