python - feedparser 出现 UndeclaredNamespace 错误

标签 python python-2.7 feedparser

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

raise UndeclaredNamespace, "'%s' is not associated with a namespace" % givenprefix feedparser.UndeclaredNamespace: 'media' is not associated with a namespace

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

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 

所有其他提要均正常。有人可以就此给我一些建议吗。

最佳答案

我查看了 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/

相关文章:

python - Feedparser(和 urllib2)问题 : Connection timed out

python - 将字符串从 feedparser 转换为日期时间

python - 如何禁用flask app.run() 的默认消息?

python - 构建 JSON 格式时出现问题。在字典和集合之间选择?

Python 单元测试 - 'gcsfs.utils.HttpError: Anonymous caller does not have storage.objects.list access to the Google Cloud Storage bucket.'

python - 正则表达式 : How to match using previous matches?

python - 获取 python 对象以在创建时返回多个值,包括 self

Python-绘图不可用

python - 使用 csv.reader 比在 python 中编写我自己的解析器有什么优势

python - Python中使用feedparser的updated_pa​​rsed和published_pa​​rsed的区别