2013-05-25 33 views
0

我执行这些行:蟒蛇feedparser不一致的项目

import feedparser 
url = 'https://dl.dropboxusercontent.com/u/5724095/TutorialFeed/feed.xml' 
feed = feedparser.parse(url) 
items = feed['items'] 
print items[0]['links'][1]['href] 

即采用这种feedparser module。这是有问题的RSS源的采样区块:

<item> 
    <title>More Android Annotations</title> 
    <link>http://youtu.be/77pPceVicNI</link> 
    <description><![CDATA[Walkthrough that goes a little bit more indepth to show you the things that <a href="http://androidannotations.org">AndroidAnnotations</a> can do for you as an application developer. <br /><a href="https://dl.dropboxusercontent.com/u/5724095/TutorialFeed/StackSitesAnnotations.mp4">Direct download link <i>(rightclick and choose save as)</i></a>]]></description> 
    <image> 
     <url>https://dl.dropboxusercontent.com/u/5724095/images/Githubpics/moreAnnotations.png</url> 
     <link>https://github.com/FoamyGuy/StackSites</link> 
     <title>More Android Annotations</title> 
    </image> 
    </item> 

我试图获得该项目的https://github.com/FoamyGuy/StackSites部分。在我的本地电脑上(win7 python 2.6),这个工作正常。但是当我在控制台上执行相同的代码时,我的github链接是pythonanywhere.com而不是我的github链接,我得到https://dl.dropboxusercontent.com/u/5724095/TutorialFeed/StackSitesAnnotations.mp4这是在说明中CDATA结尾附近包含的mp4链接。

在两台机器上items[0]['links']只包含2个元素(索引0和1),但索引1处的字符串的值在两台机器上不同。为什么feedparser会在一台机器上给我不同的值而不是另一台呢?

我已经在pythonanywhere上打印了整个items[0],并且我的github链接根本不包含在其中。是否有一些参数可以用来改变feed解析的方式,这样我就可以正确地获取github链接了吗?

是否有一些其他的feed解析模块对我更好,希望在机器上更一致?

+0

它可能是某种地理位置的东西? PythonAnywhere服务器在美国,也许你住在某个地方,服务器根据IP返回不同的结果? – hwjp

+0

我住在美国,(我认为pythonanywhere是基于英国的)。但无论哪种方式,它不应该是一个地理定位问题,因为有问题的XML是在我的控制之下,不应该根据地区而改变。 – FoamyGuy

回答

0

已经尝试用你的饲料,它看起来像每个项目在“链接”两个条目,但是看起来他们是一致的不同 - 一个将有rel="alternate",以及一个将rel="enclosure"

In [8]: items[0]['links'] 
Out[8]: 
[{'href': u'http://youtu.be/NL7szHeEiCs', 
    'rel': u'alternate', 
    'type': u'text/html'}, 
{u'href': u'https://dl.dropboxusercontent.com/u/5724095/TutorialFeed/ButtonExample.mp4', 
    'rel': u'enclosure'}] 

In [9]: items[1]['links'] 
Out[9]: 
[{'href': u'http://youtu.be/77pPceVicNI', 
    'rel': u'alternate', 
    'type': u'text/html'}, 
{u'href': u'https://dl.dropboxusercontent.com/u/5724095/TutorialFeed/StackSitesAnnotations.mp4', 
    'rel': u'enclosure'}] 

那么,你能用它来获得你想要的那个吗?

def get_alternate_link(item): 
    for link in item.links: 
     if link.get('rel') == 'alternate': 
      return link.get('href') 
+0

今天晚些时候我可以测试它。我会告诉你。 – FoamyGuy