使用 Python Feedparser 解析嵌套在 MRSS 提要中的字典中的 key:values
Parse out key:values in dictionary nested in MRSS feed using Python Feedparser
我查看了 Python feedparser 文档并做了足够的谷歌搜索,但没有找到任何看起来像我正在使用的示例提要:
http://smrss.neulion.com/u/nhl/mrss/sights-and-sounds/vod.xml
我要访问的是提要中每个项目的 media:group --> media:content 元素中的 mp4 URL。
到目前为止,这是我的代码:
#! /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' 键处的值?
如果 item.media_content 总是一个只有一个字典的列表,就这样做:
for key, val in item.media_content[0].iteritems():
print key, val
我建议使用 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',
我查看了 Python feedparser 文档并做了足够的谷歌搜索,但没有找到任何看起来像我正在使用的示例提要:
http://smrss.neulion.com/u/nhl/mrss/sights-and-sounds/vod.xml
我要访问的是提要中每个项目的 media:group --> media:content 元素中的 mp4 URL。
到目前为止,这是我的代码:
#! /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' 键处的值?
如果 item.media_content 总是一个只有一个字典的列表,就这样做:
for key, val in item.media_content[0].iteritems():
print key, val
我建议使用 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',