解析 XML 时所有 nodeValue 字段都为 None [英] All nodeValue fields are None when parsing XML

查看:31
本文介绍了解析 XML 时所有 nodeValue 字段都为 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 构建一个简单的基于 Web 的 RSS 阅读器,但是我在解析 XML 时遇到了问题.我从在 Python 命令行中尝试一些东西开始.

<预><代码>>>>从 xml.dom 导入 minidom>>>导入 urllib2>>>url ='http://www.digg.com/rss/index.xml'>>>xmldoc = minidom.parse(urllib2.urlopen(url))>>>channelnode = xmldoc.getElementsByTagName("channel")>>>channelnode = xmldoc.getElementsByTagName("channel")>>>titlenode = channelnode[0].getElementsByTagName("title")>>>打印标题节点[0]<DOM元素:0xb37440处的标题>>>>打印 titlenode[0].nodeValue没有任何

我玩了一段时间,但所有内容的 nodeValue 似乎都是 None.然而,如果您查看 XML,就会发现那里肯定有值.我做错了什么?

解决方案

对于 RSS 提要,您应该尝试 Universal Feed Parser 库.它极大地简化了 RSS 提要的处理.

导入 feedparserd = feedparser.parse('http://www.digg.com/rss/index.xml')标题 = d.channel.title

I'm building a simple web-based RSS reader in Python, but I'm having trouble parsing the XML. I started out by trying some stuff in the Python command line.

>>> from xml.dom import minidom
>>> import urllib2 
>>> url ='http://www.digg.com/rss/index.xml'
>>> xmldoc = minidom.parse(urllib2.urlopen(url))
>>> channelnode = xmldoc.getElementsByTagName("channel")
>>> channelnode = xmldoc.getElementsByTagName("channel")
>>> titlenode = channelnode[0].getElementsByTagName("title")
>>> print titlenode[0]
<DOM Element: title at 0xb37440> 
>>> print titlenode[0].nodeValue 
None

I played around with this for a while, but the nodeValue of everything seems to be None. Yet if you look at the XML, there definitely are values there. What am I doing wrong?

解决方案

For RSS feeds you should try the Universal Feed Parser library. It simplifies the handling of RSS feeds immensly.

import feedparser
d = feedparser.parse('http://www.digg.com/rss/index.xml')
title = d.channel.title

这篇关于解析 XML 时所有 nodeValue 字段都为 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆