Shreedhar Manek
Shreedhar Manek

Reputation: 157

How do I get content from xml tags when the same tag is used multiple times using lxml in python?

Let's say I have the following content -

<test> This is some text </test>
<test> This is other text </test>

Now I want to extract the content from both tags, but be able to refer to them as tag-1 and tag-2 so that I can work with them separately. How do I do that?

Upvotes: 0

Views: 344

Answers (1)

Ben
Ben

Reputation: 2472

Use root.findall("test"). This will return a list of elements -- use indices rather than different tag names.

root.findall("test")[0].text --> "This is some text"
root.findall("test")[1].text --> "This is other text"

Upvotes: 1

Related Questions