GoogleAppEngineで使用するPython2.7でアプリを作成しようとしています。RSSフィードを解析してデータをデータベースに保存したいのですが、「enclosure」タグの「url」属性の値を取得すると、一貫性のない動作が発生します。私はコーディングに不慣れであり、これに関する助けをいただければ幸いです。
私は2つのRSSフィードを持っています:
フィードA: http: //youhadtobethere.libsyn.com/rss
<item>
<title>Episode 97: Yannis Pappas</title>
<pubDate>Thu, 07 Feb 2013 19:38:00 +0000</pubDate>
<guid isPermaLink="false"><![CDATA[1364808bb99fe6bdb71b16333530076f]]></guid>
<link><![CDATA[http://youhadtobethere.libsyn.com/episode-97-yannis-pappas]]></link>
<media:thumbnail url="http://assets.libsyn.com/item/2210463" />
<description><![CDATA[<div>This week, Nikki and Sara marvel at how, two weeks in, they've already gotten used to the process of making their television show. Sara recently saw <i>Django Unchained</i> in a now-rare moment of free time and when she says she liked it, Brooklyn-born comic and certified "grown man" (see <a href="http://splitsider.com/2013/02/you-had-to-be-there-96-jessimae-peluso/">ep. 96</a>) <a href="http://ditchfilms.com/?page_id=2">Yannis Pappas</a> (<a href="http://www.youtube.com/watch?v=PWSRHNvTSIU"><i>Modern Comedian</i></a>, <a href="https://twitter.com/yannispappas">Twitter</a>) offers his wholehearted agreement. After flinging fury at Brooklyn's bogus new neighborhoods, Philly's sports obsessions, and Beantown's general demeanor, Yannis tells the story of the shooting that shoved him into maturity early on in his career. The trio muse a bit on their futile little legacies but soon leap into a joyous edition of Talking Pee that includes a <a href="http://whatsongamidancingto.com/">previral guessing game</a> on YouTube, a <a href="http://www.fitbit.com/">playful pedometer</a> on everyone's waistband, Yannis's modest dog zoo, and Nikki's upcoming appearance on <a href="http://www.comedycentral.com/shows/the-burn-with-jeff-ross"><i>The Burn</i></a>. Check her out this Tuesday (2/12) on Comedy Central at 10.30pm/9.30c...<br /><br /></div>
<p>...conveniently right before you flip over to <b><i>Nikki & Sara LIVE</i></b> on <b>MTV </b>at <b>11pm/10c</b>. <i>Nikki & Sara LIVE</i>: like a podcast for your eyes!</p>]]></description>
<enclosure length="61227675" type="audio/mpeg" url="http://traffic.libsyn.com/youhadtobethere/YHTBT_97_YannisPappas.mp3" />
<itunes:duration>01:03:47</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>pappas,sara,nikki,schaefer,glaser,yannis</itunes:keywords>
<itunes:subtitle><![CDATA[This week, Nikki and Sara marvel at how, two weeks in, they've already gotten used to the process of making their television show. Sara recently saw Django Unchained in a now-rare moment of free time and when she says she liked it, Brooklyn-born...]]></itunes:subtitle>
</item>
フィードB:http ://smodcast.com/channels/bagged-boarded-live/feed/
<item>
<title>
Bagged & Boarded Live 146: All Superheroes Must Pod
</title>
<link>
http://smodcast.com/episodes/all-superheroes-must-pod/
</link>
<comments>
http://smodcast.com/episodes/all-superheroes-must-pod/#comments
</comments>
<pubDate>Tue, 22 Jan 2013 00:08:35 +0000</pubDate>
<dc:creator>Editor</dc:creator>
<category>
<![CDATA[ Episodes ]]>
</category>
<guid isPermaLink="false">http://smodcast.com/?p=12999</guid>
<description>
<![CDATA[
In which Matt sits down with Jason Trost (The Fp) and Lucas Till (X-Men First Class) for a chat about their new film All Superheroes Must Die
]]>
</description>
<content:encoded>
<![CDATA[
In which Matt sits down with Jason Trost (The Fp) and Lucas Till (X-Men First Class) for a chat about their new film All Superheroes Must Die
]]>
</content:encoded>
<wfw:commentRss>
http://smodcast.com/episodes/all-superheroes-must-pod/feed/
</wfw:commentRss>
<slash:comments>0</slash:comments>
<enclosure url="http://api.soundcloud.com/tracks/75907996/stream.mp3?client_id=a427c512429c9c90e58de7955257879c" length="0" type="audio/mpeg"/>
</item>
コードスニペット:
import urllib
from lxml import etree
rss = etree.parse(urllib.urlopen(feedUrl))
show = rss.getroot()
for episode in show.iter('item'):
mediaUrl = episode.xpath('enclosure/@url')
これにより、url属性の値が唯一のアイテムであるリストが返されます。フィードAで実行する場合、代わりにmediaUrl = Episode.xpath('enclosure / @ url')[0]を使用するか、mediaUrl =mediaUrl[0]を使用してそのURLを文字列として保存できます。ただし、フィードBでは、どちらもエラーを生成します。IndexError:リストインデックスが範囲外です。Feed BIから返されたリストでlen(mediaUrl)を使用すると、結果が1になります。これは、URLを含むリストが返されたことを意味しますが、そのリストからURLを取得しようとするとIndexErrorが生成されます。 。
私はもう試した:
enclosure = episode.find('enclosure')
mediaUrl = enclosure.get('url')
そして、これはURLをフィードAから文字列として適切に取得しますが、AttributeErrorを生成します。'NoneType'オブジェクトには属性'get'エラーがフィードBにありません。次を使用しても同じ動作が発生します。
mediaUrl = episode.find('enclosure').attrib['url']
フィードAから文字列としてURLを正しく返し、AttributeErrorを生成します。'NoneType'オブジェクトにはフィードBからの属性'attrib'がありません。
最後の2つの方法を使用してフィードAからURLを簡単に取得できる理由を説明するために、2つのRSSフィードのレイアウトに明らかな違いは見られませんが、フィードBではまったく見られません。理由がわかりません。最初の方法を使用してフィードAから返されたリストからURLを抽出できますが、フィードBから返されたリストからは抽出できません。誰か助けてもらえますか?