【问题标题】:Access openSearch:totalResults using feedparser使用 feedparser 访问 openSearch:totalResults
【发布时间】:2012-07-28 16:45:38
【问题描述】:

如何使用 feedparser 检索 openSearch:totalResults 属性?

我有一个博客 API 结果看起来有点像这样(我已经剪掉了一些东西以使其紧凑)

 <feed xmlns='http://www.w3.org/2005/Atom'
  xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
  xmlns:gd='http://schemas.google.com/g/2005'
  gd:etag='W/"CUYMQ348fyp7ImA9WB9UFkU."'>
 <id>tag:blogger.com,1999:blog-blogID.postpostID..comments</id>
 <updated>2007-12-14T17:46:22.077-08:00</updated>
 <title>Comments on Lizzy's Diary: Quite disagreeable</title>  
 <generator version='7.00'
   uri='http://www.blogger.com'>Blogger</generator>
 <openSearch:totalResults>1</openSearch:totalResults>
 <openSearch:startIndex>1</openSearch:startIndex>

目前,我正在做类似的事情

req = urllib.urlopen(url)
urlContent = req.read()            
feed = feedparser.parse(urlContent)
print feed.feed['openSearch_totalResults']

我在上面的代码中得到一个属性不存在的错误。我已通过运行确认提要解析器具有命名空间

print feed['namespaces']

这给了我

{'': u'http://www.w3.org/2005/Atom', u'gd': u'http://schemas.google.com/g/2005', u'thr': u'http://purl.org/syndication/thread/1.0', u'openSearch': u'http://a9.com/-/spec/opensearchrss/1.0/'}

【问题讨论】:

    标签: feedparser


    【解决方案1】:

    大小写有问题:应该是opensearch_totalresults,而不是openSearch_totalResults。因此,正确的行是:

    print feed.feed['opensearch_totalresults']
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多