[英]Python feedparser return URL of first media item in first entry
I'm working with python for the first time and I am a bit stuck. 我是第一次使用python,但有点卡住了。
Using feedparser to parse a RSS feed, I want to get the URL of the first media item of entry 0 and load it into a variable. 使用feedparser解析RSS feed,我想获取条目0的第一个媒体项的URL并将其加载到变量中。
The below seems to work, but I have to hit enter twice to run it and it returns the URLs for ALL media items in the entry 0, where I only want the first (16x9) image URL. 下面的代码似乎有效,但是我必须按回车两次才能运行它,并且它返回条目0中所有媒体项目的URL,在这里我只需要第一个(16x9)图像URL。
>>> import feedparser
>>> d = feedparser.parse(http://www.abc.net.au/news/feed/45910/rss)
>>> for content in d.entries[0].media_content: print content['url']
-link to where i got the code above -链接到我上面的代码所在的位置
RSS XML: RSS XML:
<media:group>
<media:description>French fighter jets take off to drop bombs on the Islamic State stronghold of Raqqa in Syria. (Supplied)</media:description>
<media:content url="http://www.abc.net.au/news/image/6943630-16x9-2150x1210.jpg" medium="image" type="image/jpeg" width="2150" height="1210"/>
<media:content url="http://www.abc.net.au/news/image/6943630-4x3-940x705.jpg" medium="image" type="image/jpeg" width="940" height="705"/>
<media:content url="http://www.abc.net.au/news/image/6943630-3x2-940x627.jpg" medium="image" type="image/jpeg" width="940" height="627" isDefault="true"/>
<media:content url="http://www.abc.net.au/news/image/6943630-3x4-940x1253.jpg" medium="image" type="image/jpeg" width="940" height="1253"/>
<media:content url="http://www.abc.net.au/news/image/6943630-1x1-1400x1400.jpg" medium="image" type="image/jpeg" width="1400" height="1400"/>
<media:thumbnail url="http://www.abc.net.au/news/image/6943630-4x3-140x105.jpg" width="140" height="105"/>
</media:group>
Looks like this when run in python: 在python中运行时看起来像这样:
>>> for content in d.entries[0].media_content: print content['url']
...
http://www.abc.net.au/news/image/6943630-16x9-2150x1210.jpg
http://www.abc.net.au/news/image/6943630-4x3-940x705.jpg
http://www.abc.net.au/news/image/6943630-3x2-940x627.jpg
http://www.abc.net.au/news/image/6943630-3x4-940x1253.jpg
http://www.abc.net.au/news/image/6943630-1x1-1400x1400.jpg
>>>
Quick answer: 快速回答:
url = d.entries[0].media_content[0]['url']
d.entries[n].media_content
is a list full of dicts, so you can just get the first item in that list and store the value at "url" in a variable. d.entries[n].media_content
是一个充满字典的列表,因此您只需获取该列表中的第一项并将值存储在变量中的“ url”即可。
Here's how it looks in the Python shell: 这是它在Python Shell中的外观:
>>> import feedparser
>>> d = feedparser.parse("http://www.abc.net.au/news/feed/45910/rss")
>>> url = d.entries[0].media_content[0]['url']
>>> print url
http://www.abc.net.au/news/image/6943798-16x9-2150x1210.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.