Reputation: 10133
I want to access a tag called as "name"
such as:
<contact><name>Yesügey</name><lastName>Yeşil</lastName><phone>+90 333 9695395</phone></contact>
Since "name"
is a property of a BeautifulSoup tag object, I cannot access the child tag name
:
>>> c1
<contact><name>Yesügey</name><lastname>Yeşil</lastname><phone>+90 333 9695395</p
hone></contact>
>>> c1.name
'contact'
>>> c1.lastname
<lastname>Yeşil</lastname>
Upvotes: 17
Views: 4116
Reputation: 7743
Late answer, but I had the same issue when trying to find a <textarea name=COMMENTS>
My solution:
node = soup.find("textarea", attrs={"name": "COMMENTS"}
Upvotes: 0
Reputation: 7380
You can use the .find()
method:
Examples:
c2.find('name')
<name>Yesügey</name>
c2.find('name').contents
Yesügey
Upvotes: 2
Reputation: 21446
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
Upvotes: 19
Reputation: 1806
Described is two different stratgies of accessing xml element name
>>> xmlstring = '<contact><name>Yesügey</name><lastName>Yeşil</lastName><phone>+90 333 9695395</phone></contact>'
>>> from BeautifulSoup import BeautifulSoup as Soup
>>> f = Soup(xmlstring)
>>> f.find('name')
<name>YesĂźgey</name>
>>> f.contact.name
u'contact'
>>>
Upvotes: 0
Reputation: 59974
Here's what I got:
from bs4 import BeautifulSoup as BS
soup = '<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 :).
Upvotes: 5