您好,我在使用 Feedparser 时遇到以下错误:
raise UndeclaredNamespace, "'%s' is not associated with a namespace" % givenprefix feedparser.UndeclaredNamespace: 'media' is not associated with a namespace
我已经设法将它缩小到一个提要,这是原因:
我正在编写一个网络抓取工具,它使用 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
所有其他提要均正常。有人可以就此给我一些建议吗。
最佳答案
我查看了 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 feed 是错误形成的。解决它的一种方法是为媒体使用公共(public)命名空间。我在 http://www.rssboard.org/media-rss 找到了一份引用资料.他们举了一个例子:
xmlns:media="http://search.yahoo.com/mrss/"
关于python - feedparser 出现 UndeclaredNamespace 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25070905/