使用 urllib 和 minidom 获取 XML 数据

发布于 2024-09-11 22:44:46 字数 566 浏览 1 评论 0原文

我正在尝试从 XML 服务中获取数据...这个。

http://xmlweather.vedur .is/?op_w=xml&type=forec&lang=is&view=xml&ids=1

我正在使用 urrlib 和 minidom,但似乎无法使其工作。我使用 minidom 处理文件而不是 url。

这是我尝试使用的代码

xmlurl = 'http://xmlweather.vedur.is'
xmlpath = xmlurl + '?op_w=xml&type=forec&lang=is&view=xml&ids=' + str(location)
xmldoc = minidom.parse(urllib.urlopen(xmlpath))

有人可以帮助我吗?

I'm trying to fetch data from a XML service... this one.

http://xmlweather.vedur.is/?op_w=xml&type=forec&lang=is&view=xml&ids=1

I'm using urrlib and minidom and i can't seem to make it work. I've used minidom with files and not url.

This is the code im trying to use

xmlurl = 'http://xmlweather.vedur.is'
xmlpath = xmlurl + '?op_w=xml&type=forec&lang=is&view=xml&ids=' + str(location)
xmldoc = minidom.parse(urllib.urlopen(xmlpath))

Can anyone help me?

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

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

发布评论

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

评论(4

深海蓝天 2024-09-18 22:44:46

以下内容应该有效(或者至少让您清楚地了解出了什么问题):

from xml.dom.minidom import parse
import urllib

xmlurl = 'http://xmlweather.vedur.is'
xmlpath = xmlurl + '?op_w=xml&type=forec&lang=is&view=xml&ids=' + str(location)
try:
    xml = urllib.urlopen(xmlpath)
    dom = parse(xml)
except e as Exception:
    print(e)

The following should work (or at least give you a strong idea about what is going wrong):

from xml.dom.minidom import parse
import urllib

xmlurl = 'http://xmlweather.vedur.is'
xmlpath = xmlurl + '?op_w=xml&type=forec&lang=is&view=xml&ids=' + str(location)
try:
    xml = urllib.urlopen(xmlpath)
    dom = parse(xml)
except e as Exception:
    print(e)
一杯敬自由 2024-09-18 22:44:46

parse() 正在查找一个文件,并且您给它一个字符串。还有另一个类叫做 parsestring()

尝试:

from xml.dom.minidom import parseString
import urllib2
xml = urllib2.urlopen(xmlpath)
dom = parseString(xml.read())

The parse() is looking for a file and you're giving it a string. There is another class called parsestring()

try:

from xml.dom.minidom import parseString
import urllib2
xml = urllib2.urlopen(xmlpath)
dom = parseString(xml.read())
徒留西风 2024-09-18 22:44:46

试试这个:

f = urllib.urlopen(xmlpath)
html = f.read()
xmldoc = minidom.parse(html)

Try this:

f = urllib.urlopen(xmlpath)
html = f.read()
xmldoc = minidom.parse(html)
孤独难免 2024-09-18 22:44:46

我刚刚做了类似的事情,并遇到了你的问题。

就我而言,我认为 minidom.parse 已损坏,因为我收到语法错误。事实证明语法错误在我的 xml 文档中 - 跟踪并没有让这一点非常清楚。

如果您在使用 minidom.parse 或 minidom.parseString 时遇到语法错误,请务必检查您的源文件。

I've just been doing something similar, and came across your question.

In my case, I thought that minidom.parse was broken because I was getting syntax errors. It turns out the syntax errors were in my xml document though - the trace didn't make that very clear.

If you're getting syntax errors with minidom.parse or minidom.parseString, make sure to check your source file.

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