从网页解析XML

时间:2017-10-16 15:17:52

标签: python xml xml-parsing

我想解析一个XML文件。如果我的文件就像“file.xml”那么没关系

我在Python中使用这些命令:

from lxml import etree

tree = etree.parse("C:/file.xml")

但是,我现在有一个新目标,目标是一个Web服务器。此服务器生成具有临时值的XML文本。可能是来自URL的问题,因为它没有以“.xml”结尾? 该网址类似于“http://ip/xml_render”。

但是,在python中,我有错误:

lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1

实际上,我的网页并不像源代码那样完整,它看起来像这样:

enter image description here

那么,问题是关于URL还是网页?

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要先从网上抓取它,试试这个

from urllib.request import urlopen

xml = urlopen('www.yourwebsite.com')
tree = etree.parse(xml)
相关问题