1
我正在第一次使用python,並且我有點卡住了。Python feedparser返回第一個條目中第一個媒體項目的URL
使用feedparser解析一個RSS提要,我想進入0的第一媒體項目的URL,並將其加載到一個變量。下面
似乎工作,但我打兩次ENTER運行它,它返回的URL在入口0,在這裏我只希望第一(16:9)圖像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:
<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>
看起來這在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
>>>
THANKYOU!我開始玩增加另一個索引,但我一定在某處發生了一個簡單的錯誤。 –
沒問題!您可能還需要確保它是帶有一些正則表達式的16x9圖像,因爲您無法保證RSS將始終提供該圖像,但這應該很容易。 –