G
G
gregorypetrov2019-04-05 15:59:53
Python
gregorypetrov, 2019-04-05 15:59:53

LXML: how to get all child elements of a particular type?

Parsing XML file using LXML (Python). The file contains users with attributes, and each user can have several child images :

<user internal-id="1233232323">
  <name>Alex</name>
  <age>23</age>
  <image id="1">http://example.com/images/img1.jpg</image>
  <image id="2">http://example.com/images/img2.jpg</image>
  <image id="3">http://example.com/images/img3.jpg</image>
  <image id="4">http://example.com/images/img4.jpg</image>
  <image id="5">http://example.com/images/img5.jpg</image>
</user>

<user internal-id="24323423423">
  <name>Daniel</name>
  <age>31</age>
  <image id="1">http://example.com/images/img1.jpg</image>
  <image id="2">http://example.com/images/img2.jpg</image>
  <image id="3">http://example.com/images/img3.jpg</image>
  <image id="4">http://example.com/images/img4.jpg</image>
  <image id="5">http://example.com/images/img5.jpg</image>
</user>

и т.д.

Parsing:
root = etree.fromstring(data)

users = []
for user in root.getchildren():
        print user.get('internal-id')

        #а здесь нужно перебрать и вывести все относящиеся к этому юзеру картинки.
        #Не понимаю, как это сделать? Подскажите, пожалуйста.</b>

How to extract the urls of all child images from each user ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick V, 2019-04-06
@half-life

Something like this
import xml.etree.cElementTree as ET
from typing import List
from typing import NamedTuple
from typing import Tuple


class User(NamedTuple):
    internal_id: int
    name: str
    age: int
    images: Tuple[str]


XML: str = """
<root>
<user internal-id="1233232323">
  <name>Alex</name>
  <age>23</age>
  <image id="1">http://example.com/images/img1.jpg</image>
  <image id="2">http://example.com/images/img2.jpg</image>
  <image id="3">http://example.com/images/img3.jpg</image>
  <image id="4">http://example.com/images/img4.jpg</image>
  <image id="5">http://example.com/images/img5.jpg</image>
</user>

<user internal-id="24323423423">
  <name>Daniel</name>
  <age>31</age>
  <image id="1">http://example.com/images/img1.jpg</image>
  <image id="2">http://example.com/images/img2.jpg</image>
  <image id="3">http://example.com/images/img3.jpg</image>
  <image id="4">http://example.com/images/img4.jpg</image>
  <image id="5">http://example.com/images/img5.jpg</image>
</user>
</root>
"""

if __name__ == '__main__':
    users: List[User] = []
    root = ET.fromstring(XML)
    for user in root:
        user_data = tuple(tag.text for tag in user)
        users.append(
            User(
                internal_id=user.get('internal-id'),
                name=user_data[0],
                age=user_data[1],
                images=user_data[2:]
            )
        )

[User(internal_id='1233232323', name='Alex', age='23', images=('example.com/images/img1.jpg', 'example.com/images/img2.jpg', 'example.com/images/img3.jpg', 'example.com/images/img4.jpg', 'example.com/images/img5.jpg')),
User(internal_id='24323423423', name='Daniel', age='31', images=('example.com/images/img1.jpg', 'example.com/images/img2.jpg', 'example.com/images/img3.jpg', 'example.com/images/img4.jpg', 'example.com/images/img5.jpg'))]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question