使用 urllib 和 minidom 获取 XML 数据
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下内容应该有效(或者至少让您清楚地了解出了什么问题):
The following should work (or at least give you a strong idea about what is going wrong):
parse() 正在查找一个文件,并且您给它一个字符串。还有另一个类叫做 parsestring()
尝试:
The parse() is looking for a file and you're giving it a string. There is another class called parsestring()
try:
试试这个:
Try this:
我刚刚做了类似的事情,并遇到了你的问题。
就我而言,我认为 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.