使用xml.etree.ElementTree和来自Web的XML文件“找不到元素”

时间:2011-04-12 20:05:47

标签: python xml elementtree

这是Python的来源:

fsock = urllib2.urlopen('http://eprints.soton.ac.uk/cgi/exportview/divisions/uos-fp/2009/XML/uos-fp_2009.xml')

doc=et.parse(fsock)

当我尝试运行此操作时,会出现以下错误:

Traceback (most recent call last):
  File "C:\Python27\reading and writing xml file from web1.py", line 30, in 
    doc=et.parse(fsock)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1176, in parse
    tree.parse(source, parser)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 654, in parse
    self._root = parser.close()
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1635, in close
    self._raiseerror(v)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1487, in _raiseerror
    raise err
ParseError: no element found: line 1, column 0

任何人都可以帮助解决这个问题的原因吗?

1 个答案:

答案 0 :(得分:0)

您的代码有效:

import urllib2  
from xml.etree.cElementTree import parse, dump  

fsock = urllib2.urlopen('http://eprints.soton.ac.uk/cgi/exportview/divisions/uos-fp/2009/XML/uos-fp_2009.xml')

doc = parse(fsock)  
dump(doc)