繁体   English   中英

如何使用 lxml 获得带有子标签的所需标签?

[英]How to get desired Tag with child Tags using 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>
<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>


在 lxml 解析器中,如何仅获取带有子节点的第二个作业标记意味着我只想获取以下数据作为 output。 请注意,这不是固定格式,它取决于 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'元素。 这很容易使用Element class 的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_elements是一个列表,其中包含所有标记为'job'的元素,按文档中出现的顺序排列。 取第二个(索引 1)。
要打印它(及其所有子元素),您可以使用etree.tostring function。 这将返回一个二进制字符串,因此要在控制台上很好地显示它,您可能需要将其解码为 ascii。

output = etree.tostring(job_elements[1], pretty_print=True)
print(output.decode('ascii'))

更多细节

etree.parse()返回一个ElementTree object。 使用getroot()你会得到一个Element object 从ElementTree的根开始( Element有更多的方法)。 这条线实际上是不需要的,因为你需要iter方法并且ElementTree也有一个 iter 方法,我添加只是作为习惯的力量。 但是,如果您要对树进行一些额外的操作,拥有Element而不是ElementTree可能会很有用。

job_elements = list(root.iter('job'))关键是iter方法 它以文档顺序沿子树中的元素返回一个迭代器(有关更多详细信息,请参阅链接的文档)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM