解析XML时所有nodeValue字段均为None

发布于 2024-07-13 04:10:10 字数 733 浏览 6 评论 0原文

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

>>> 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

我对此进行了一段时间的研究,但所有内容的 nodeValue 似乎都是 None。 然而,如果您查看 XML,就会发现其中肯定存在值。 我究竟做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

|煩躁 2024-07-20 04:10:11

这是您正在寻找的语法:

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

请注意,节点值是节点本身的逻辑后代。

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.

远昼 2024-07-20 04:10:10

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文