All nodeValue fields are None when parsing XML

◇◆丶佛笑我妖孽 提交于 2019-12-04 21:56:50

问题


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?


回答1:


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



回答2:


This is the syntax you are looking for:

>>> print titlenode[0].firstChild.nodeValue
digg.com: Stories / Popular

Note that the node value is a logical descendant of the node itself.



来源:https://stackoverflow.com/questions/479751/all-nodevalue-fields-are-none-when-parsing-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!