【问题标题】:Parse out key:values in dictionary nested in MRSS feed using Python Feedparser使用 Python Feedparser 解析嵌套在 MRSS 提要中的字典中的键:值
【发布时间】:2015-05-01 18:23:09
【问题描述】:

我已经查看了 Python feedparser 文档并进行了足够的谷歌搜索,但没有找到任何与我正在使用的内容相似的示例提要:

http://smrss.neulion.com/u/nhl/mrss/sights-and-sounds/vod.xml

我试图访问的是媒体中的 mp4 URL:group --> media:content 供稿中每个项目中的元素。

到目前为止,这是我的代码:

#! /usr/bin/python
# -*- coding: utf-8 -*-

import feedparser

d = feedparser.parse('http://smrss.neulion.com/u/nhl/mrss/sights-and-sounds/vod.xml')

for index,item in enumerate(d.entries):
    if index >= 4:
        print item.title
        print item.media_content
        print item.summary

item.media_content 打印到终端的是:

[{'duration': u'150', 'url': u'http://smrss.neulion.com/spmrss/s/nhl/vod/flv/2015/04/19/811204_20150418_PIT_NYR_WIRELESS_1800_sd.mp4', 'type': u'video_sd.mp4'}]

这是列表中的字典,是吗?在我的 for 循环中迭代这个字典的最佳方法是什么,以便我可以在“url”键处提取值?

【问题讨论】:

    标签: python list dictionary feedparser


    【解决方案1】:

    如果 item.media_content 始终是一个包含一个字典的列表,只需这样做:

    for key, val in item.media_content[0].iteritems():
        print key, val
    

    【讨论】:

    • 非常感谢您打破它!我错过了 [0] - 我假设我们需要告诉 Python 列表所在的索引,即使提要中的每个项目只有一个 media_content 列表?
    • 不客气 :-) 实际上,您首先告诉 Python 获取列表的第一项。
    【解决方案2】:

    我建议使用BeautifulSoup

    import urllib
    from bs4 import BeautifulSoup
    url = "http://smrss.neulion.com/u/nhl/mrss/sights-and-sounds/vod.xml"
    vod = urllib.urlopen(url)
    
    
    
    In [1752]: [i['url'] for i in soup.findAll('media:content') if i.has_attr('url')]
    Out[1752]: 
    ['http://smrss.neulion.com/spmrss/s/nhl/vod/flv/2015/04/30/817293_C150008B_20150428_ROUND_ONE_WIRELESS_RECAP_SHORT_5000_sd.mp4',
     'http://smrss.neulion.com/spmrss/s/nhl/vod/flv/2015/04/28/816995_20150427_NHL_Playoff_Access_NYI_WSH_GM7_5000_sd.mp4',
     'http://smrss.neulion.com/spmrss/s/nhl/vod/flv/2015/04/26/816230_20150426_WIRELESS_RECAP_5000_sd.mp4',
     'http://smrss.neulion.com/spmrss/s/nhl/vod/flv/2015/04/25/815823_20150425_WIRELESS_GM5_OTT_5000_sd.mp4',
    

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2023-03-28
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      相关资源
      最近更新 更多