gpt4 book ai didi

python - lxml:来自 URL 的一些 XML 给出了这个 lxml.etree.XMLSyntaxError

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:22 27 4
gpt4 key购买 nike

我有一个脚本,可以从 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

通过在解析之前打印文件名/U​​RL 进行调试。然后您将看到导致错误的文件。

另外,阅读错误信息:

lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1

这表明下载的 XML 文件是空的。确定导致问题的 URL 后,请尝试下载文件并检查其内容。我怀疑它可能是空的。

您可以在解析时使用 try/except block 来忽略有问题的文件(空文件或其他语法无效的文件):

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 - lxml:来自 URL 的一些 XML 给出了这个 lxml.etree.XMLSyntaxError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32960412/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com