lxml:来自 URL 的一些 XML 给这个 lxml.etree.XMLSyntaxError
lxml: some XML from URL give this lxml.etree.XMLSyntaxError
我有一个脚本,可以从 URL 列表中的 XML 文件中提取一些术语。
所有 URL 都可以访问 XML 数据。
它在第一次正确打开、解析和提取时工作正常,但随后在过程中被一些 XML 文件中断并出现此错误:
File "<stdin>", line 18, in <module>
File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204)
File "parser.pxi", line 1555, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82511)
File "parser.pxi", line 1585, in lxml.etree._parseFilelikeDocument (src/lxml/lxml.etree.c:82832)
File "parser.pxi", line 1468, in lxml.etree._parseDocFromFilelike (src/lxml/lxml.etree.c:81688)
File "parser.pxi", line 1024, in lxml.etree._BaseParser._parseDocFromFilelike (src/lxml/lxml.etree.c:78735)
File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75363)
File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74696)
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
根据我的搜索,可能是因为某些 XML 文件有空格,但我不确定这是否是问题所在。我不知道是哪个文件出错了。
有没有办法解决这个错误?
这是我的脚本:
URLlist = ["http://www.uniprot.org/uniprot/"+x+".xml" for x in IDlist]
for id, item in zip(IDlist, URLlist):
goterm_location = []
goterm_function = []
goterm_process = []
location_list[id] = []
function_list[id] = []
biological_list[id] = []
try:
textfile = urllib2.urlopen(item);
except urllib2.HTTPError:
print("URL", item, "could not be read.")
continue
#Try to solve empty line error#
tree = etree.parse(textfile);
#root = tree.getroot()
for node in tree.iter('{http://uniprot.org/uniprot}dbReference'):
if node.attrib.get('type') == 'GO':
for child in node:
value = child.attrib.get('value');
if value.startswith('C:'):
goterm_C = node.attrib.get('id')
if goterm_C:
location_list[id].append(goterm_C);
if value.startswith('F:'):
goterm_F = node.attrib.get('id')
if goterm_F:
function_list[id].append(goterm_F);
if value.startswith('P:'):
goterm_P = node.attrib.get('id')
if goterm_P:
biological_list[id].append(goterm_P);
我试过:
tree = etree.iterparse(textfile, events = ("start","end"));
OR
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(textfile, parser)
没有成功。
任何帮助将不胜感激
I can't tell which files give the error
通过在解析之前打印 file/URL 的名称进行调试。然后你会看到是哪个文件导致了错误。
另外,阅读错误信息:
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
这表明下载的 XML 文件是空的。确定导致问题的 URL(s) 后,请尝试下载文件并检查其内容。我怀疑它可能是空的。
您可以在解析时使用 try/except 块忽略有问题的文件(空文件或其他语法无效文件):
try:
tree = etree.parse(textfile)
except lxml.etree.XMLSyntaxError:
print 'Skipping invalid XML from URL {}'.format(item)
continue # go on to the next URL
或者您可以通过检查 'Content-length' header 或什至通过读取 urlopen()
返回的资源来检查空文件,但我认为上面的方法更好它还会捕获其他潜在错误。
我在 Python 3.6
中收到相同的错误消息
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
在我的例子中,xml 文件不是空的。问题是因为编码,
最初使用utf-8,
from lxml import etree
etree.iterparse(my_xml_file.xml, tag='MyTag', encoding='utf-8')
将编码更改为 iso-8859-1 解决了我的问题,
etree.iterparse(my_xml_file.xml, tag='MyTag', encoding='iso-8859-1')
我有一个脚本,可以从 URL 列表中的 XML 文件中提取一些术语。 所有 URL 都可以访问 XML 数据。
它在第一次正确打开、解析和提取时工作正常,但随后在过程中被一些 XML 文件中断并出现此错误:
File "<stdin>", line 18, in <module>
File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204)
File "parser.pxi", line 1555, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82511)
File "parser.pxi", line 1585, in lxml.etree._parseFilelikeDocument (src/lxml/lxml.etree.c:82832)
File "parser.pxi", line 1468, in lxml.etree._parseDocFromFilelike (src/lxml/lxml.etree.c:81688)
File "parser.pxi", line 1024, in lxml.etree._BaseParser._parseDocFromFilelike (src/lxml/lxml.etree.c:78735)
File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75363)
File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74696)
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
根据我的搜索,可能是因为某些 XML 文件有空格,但我不确定这是否是问题所在。我不知道是哪个文件出错了。 有没有办法解决这个错误?
这是我的脚本:
URLlist = ["http://www.uniprot.org/uniprot/"+x+".xml" for x in IDlist]
for id, item in zip(IDlist, URLlist):
goterm_location = []
goterm_function = []
goterm_process = []
location_list[id] = []
function_list[id] = []
biological_list[id] = []
try:
textfile = urllib2.urlopen(item);
except urllib2.HTTPError:
print("URL", item, "could not be read.")
continue
#Try to solve empty line error#
tree = etree.parse(textfile);
#root = tree.getroot()
for node in tree.iter('{http://uniprot.org/uniprot}dbReference'):
if node.attrib.get('type') == 'GO':
for child in node:
value = child.attrib.get('value');
if value.startswith('C:'):
goterm_C = node.attrib.get('id')
if goterm_C:
location_list[id].append(goterm_C);
if value.startswith('F:'):
goterm_F = node.attrib.get('id')
if goterm_F:
function_list[id].append(goterm_F);
if value.startswith('P:'):
goterm_P = node.attrib.get('id')
if goterm_P:
biological_list[id].append(goterm_P);
我试过:
tree = etree.iterparse(textfile, events = ("start","end"));
OR
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(textfile, parser)
没有成功。 任何帮助将不胜感激
I can't tell which files give the error
通过在解析之前打印 file/URL 的名称进行调试。然后你会看到是哪个文件导致了错误。
另外,阅读错误信息:
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
这表明下载的 XML 文件是空的。确定导致问题的 URL(s) 后,请尝试下载文件并检查其内容。我怀疑它可能是空的。
您可以在解析时使用 try/except 块忽略有问题的文件(空文件或其他语法无效文件):
try:
tree = etree.parse(textfile)
except lxml.etree.XMLSyntaxError:
print 'Skipping invalid XML from URL {}'.format(item)
continue # go on to the next URL
或者您可以通过检查 'Content-length' header 或什至通过读取 urlopen()
返回的资源来检查空文件,但我认为上面的方法更好它还会捕获其他潜在错误。
我在 Python 3.6
中收到相同的错误消息lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
在我的例子中,xml 文件不是空的。问题是因为编码,
最初使用utf-8,
from lxml import etree
etree.iterparse(my_xml_file.xml, tag='MyTag', encoding='utf-8')
将编码更改为 iso-8859-1 解决了我的问题,
etree.iterparse(my_xml_file.xml, tag='MyTag', encoding='iso-8859-1')