[Python] python - lxml how to get children of element by tag name?

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a xml file that looks like this:

    <page>
    <title>title1</title>
    <subtitle>subtitle</subtitle>
    <ns>0</ns>
    <id>1</id>
    <text>hello world!@</text>
    </page>
    <page>
    <title>title2</title>
    <ns>0</ns>
    <id>1</id>
    <text>hello world</text>
    </page>


    How can I get the text of each page? Right now I have a list of each page. The following code will print the text of the second page element but not the first. Is there a way to take the child element by tag name like element['text']

    for i in pages:
    print i[3]

    Continue reading...

Compartilhe esta Página