解析输出关键字:值使用Python Feedpars嵌套在MRSS提要中的字典

2024-06-06 00:20:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我浏览了Python feedparser文档并进行了足够的google搜索,但没有找到任何与我所使用的类似的示例提要:

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

我要访问的是媒体:组-->;媒体:内容元素在源中的每个项目中。在

以下是我目前为止的代码:

#! /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_内容是:

^{pr2}$

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


Tags: andcomhttpxmlitem媒体printnhl
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',

如果item.media_内容总是包含一个字典的列表,只需执行以下操作:

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

相关问题 更多 >