How to access a tag called "name" in BeautifulSoup How to access a tag called "name" in BeautifulSoup xml xml

How to access a tag called "name" in BeautifulSoup


You can try like this,

>>> soup=BeautifulSoup.BeautifulSoup(content).findAll('name')>>> for field in soup:...     print field... <name>Yesügey</name>

Or

print soup.find('name').string


Here's what I got:

from bs4 import BeautifulSoup as BSsoup = '<contact><name>Yesügey</name><lastName>Yeşil</lastName><phone>+90 333 9695395</phone></contact>'soup = BS(soup)print soup.find('name').string# Prints YesĂźgey

So instead of calling the name tag, I simply find it and get what's inside it :).


You can use the .find() method:

Examples:

c2.find('name')

<name>Yesügey</name>

c2.find('name').contents

Yesügey