【问题标题】:UndeclaredNamespace error with feedparserfeedparser 出现未声明的命名空间错误
【发布时间】:2014-09-24 02:33:43
【问题描述】:

您好,我在使用 Feedparser 时遇到以下错误:

引发 UndeclaredNamespace,“'%s' 未与命名空间关联”% givenprefix feedparser.UndeclaredNamespace: 'media' 未关联 带有命名空间

我已设法将其缩小到一个提要,这就是原因:

http://feeds.mashable.com/Mashable

我正在编写一个网络抓取工具,它使用 Feedparser 和 Beautiful soup 从我感兴趣的各种 rss 提要中抓取新闻文章,我正在使用 Python 编写这个。

错误信息是这样的:

feed = feedparser.parse(in_feed)
File "/usr/lib/python2.7/dist-packages/feedparser.py", line 4014, in parse
saxparser.parse(source)
File "/usr/lib/python2.7/dist-packages/drv_libxml2.py", line 208, in parse
eltName,eltQName,attributesNSImpl) 
File "/usr/lib/python2.7/dist-packages/feedparser.py", line 1809, in startElementNS
raise UndeclaredNamespace, "'%s' is not associated with a namespace" % givenprefix
feedparser.UndeclaredNamespace: 'media' is not associated with a namespace 

所有其他供稿都正常。有人可以给我一些建议吗?

【问题讨论】:

  • 抛出异常时你在执行什么代码?
  • 我正在运行 'feed = feedparser.parse(in_feed)' where in_feed=feeds.mashable.com/Mashable

标签: python python-2.7 feedparser


【解决方案1】:

我查看了 Mashable 提要,它的顶部有所有导入的命名空间:

<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom' xmlns:campaign='http://organizecampaign.com/' xmlns:content='http://purl.org/rss/1.0/modules/content/' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:mash='http://mashable.com/rss' xmlns:mashable='http://mashable.com/rss' xmlns:slash='http://purl.org/rss/1.0/modules/slash/' xmlns:sy='http://purl.org/rss/1.0/modules/syndication/' xmlns:wfw='http://wellformedweb.org/CommentAPI/'>

没有为“media”定义命名空间,这就是为什么稍后遇到标签“media:thumbnail”时会抛出错误。

因此,我会说 Mashable 提要的格式错误。解决它的一种方法是为媒体使用公共命名空间。我在http://www.rssboard.org/media-rss 找到了一份参考资料。他们举了一个例子:

xmlns:media="http://search.yahoo.com/mrss/"

【讨论】:

  • 我已经就这个问题联系了 Mashable。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多