Stanislav Pavlovič
Stanislav Pavlovič

Reputation: 259

Returning attribute called "name" in BeautifulSoup4

I would need to return the value of child called "name" in an XML file. The default usage only returns the name of the tag (as the .name seems to be a BS4 function, that returns the tag's name:

for e in eventSoup.find_all('event'):
    print(e.name)

# event

Is there a way to return the the actual value of the tag?

edit: The XML is structured like this:

<event id="7">
<def_id>7</def_id>
<name>Event name</name>

Upvotes: 0

Views: 45

Answers (2)

Zroq
Zroq

Reputation: 8392

You can use find.

Events = soup.find_all("event")

for Event in Events:
    NameChild = Event.find("name")
    print (NameChild.text)

Outputs:

Event name

Upvotes: 1

Mithilesh Gupta
Mithilesh Gupta

Reputation: 2930

you need .text property.

eg:

for e in eventSoup.find_all('event'):
    nameTag = e.find('name')
    print(nameTag.text)

Upvotes: 0

Related Questions