Python 如何使用lxml通过子标记获得所需的标记?
我有一个XML文件,比如Python 如何使用lxml通过子标记获得所需的标记?,python,lxml,Python,Lxml,我有一个XML文件,比如 <?xml version="1.0" encoding="utf-8"?> <source> <publisher>Job App</publisher> <publisherurl>https://jldfsfsd.jlfdfs.com/Jobs/</publisherurl> <lastBuildDate>10-19-2015 00:00:00</lastBuildDate&
<?xml version="1.0" encoding="utf-8"?>
<source>
<publisher>Job App</publisher>
<publisherurl>https://jldfsfsd.jlfdfs.com/Jobs/</publisherurl>
<lastBuildDate>10-19-2015 00:00:00</lastBuildDate>
<job>
<title><![CDATA[Barista/Sandwich Prep]]></title>
<date><![CDATA[10-19-2015]]></date>
<referencenumber><![CDATA[83]]></referencenumber>
<url><![CDATA[https://test/Jobs/Job.aspx?JobPostingId=83&SourceId=3]]></url>
<company><![CDATA[Another Cafe]]></company>
<city><![CDATA[San Francisco]]></city>
<state><![CDATA[California]]></state>
<country><![CDATA[United States of America]]></country>
<postalcode><![CDATA[94123]]></postalcode>
<description><![CDATA[ TESTTESTTESTTESTTESTTESTTESTTEST <br> STEdsasjflsdf<p> dfjhdjlas </p>]]></description>
</job>
<job>
<title><![CDATA[MV Drivers]]></title>
<date><![CDATA[01-01-1900]]></date>
<referencenumber><![CDATA[147]]></referencenumber>
<url><![CDATA[https://sdf.dsfs.com/Jobs/Job.aspx?JobPostingId=147&SourceId=3]]></url>
<company><![CDATA[Papa Johns Pizza]]></company>
<city><![CDATA[Mountain View]]></city>
<state><![CDATA[California]]></state>
<country><![CDATA[United States of America]]></country>
<book><![CDATA[BOOKTEST]]></book>
<postalcode><![CDATA[94404]]></postalcode>
<description><![CDATA[Fun sfsf job while makingfsfup to $20/hour!]]></description>
</job>
求职应用程序
https://jldfsfsd.jlfdfs.com/Jobs/
10-19-2015 00:00:00
STEdsasjflsdfdfjhdjlas]>
在lxml解析器中,如何仅获取第二个作业标记及其子节点意味着我只想获取以下数据作为输出。注意,这不是固定格式,它取决于XML文件结构
<job>
<title><![CDATA[MV Drivers]]></title>
<date><![CDATA[01-01-1900]]></date>
<referencenumber><![CDATA[147]]></referencenumber>
<url><![CDATA[https://sdf.dsfs.com/Jobs/Job.aspx?JobPostingId=147&SourceId=3]]></url>
<company><![CDATA[Papa Johns Pizza]]></company>
<city><![CDATA[Mountain View]]></city>
<state><![CDATA[California]]></state>
<country><![CDATA[United States of America]]></country>
<postalcode><![CDATA[94404]]></postalcode>
<description><![CDATA[Fun sfsf job while makingfsfup to $20/hour!]]></description>
</job>
您可以遍历文档中的元素,并提取第二个
'job'
元素。使用元素
类的iter
方法非常容易
from lxml import etree
tree = etree.parse('data.xml') #or whatever is your file name
root = tree.getroot()
job_elements = list(root.iter('job'))
job\u elements
是一个列表,其中所有元素都按文档中的出现顺序标记为“job”。以第二个为例(索引1)。要打印它(及其所有子元素),可以使用
etree.tostring
函数。这将返回一个二进制字符串,因此要在控制台上很好地显示它,您可能需要将其解码为ascii
output = etree.tostring(job_elements[1], pretty_print=True)
print(output.decode('ascii'))
更多细节
etree.parse()
返回一个ElementTree
对象。使用getroot()
可以从ElementTree
的根开始获得一个元素
对象(元素
有更多方法)。这一行实际上是不需要的,因为你需要iter
方法,ElementTree
也有一个iter方法,我添加了这一行作为习惯的力量。但是,如果您想对树进行一些额外的操作,那么使用元素而不是元素树
可能会很有用
job\u elements=list(root.iter('job'))
键是。它按文档顺序沿子树中的元素返回迭代器(有关详细信息,请参阅链接文档)。如果您尝试了任何操作,最好显示您尝试过的内容。您能详细解释一下这些行吗
root=tree.getroot()
job\u elements=list(root.iter('job'))
实际上我不是lxml方面的专家。抱歉,格式错误!!!:(哦!!我明白了…这对我来说是完美而简单的答案!