Python Beatifulsoup标记名和属性冲突

Python Beatifulsoup标记名和属性冲突,python,beautifulsoup,Python,Beautifulsoup,BeautifulSoup元素具有.text属性(的属性版本) BeautifulSoup还允许您访问属性等标记: soup.firstparent.secondparent.dosomething #etc 现在,出于不幸但无法改变的原因,我的任务是访问元素,您可以使用以下方法访问该元素: soup.firstparent.text.dosomething #in this case, 'text' tag is child node of the 'firstparent' tag 但

BeautifulSoup元素具有
.text
属性(的属性版本)

BeautifulSoup还允许您访问属性等标记:

soup.firstparent.secondparent.dosomething #etc
现在,出于不幸但无法改变的原因,我的任务是访问
元素,您可以使用以下方法访问该元素:

soup.firstparent.text.dosomething 
#in this case, 'text' tag is child node of the 'firstparent' tag

但是,这与BeautifulSoup提供的
.text
属性相冲突。问题是-如何访问名为
text
的标记,并避免与BeautifulSoup属性冲突?

属性访问是一种方便;您仍然可以使用
.find()
搜索标记:

soup.firstparent.find('text').dosomething
.find('text')
调用将搜索第一个
标记,而不是调用BeautifulSoup
.text
属性

演示:

>>来自bs4导入组
>>>汤=美汤(“”)
…你好,世界!“”)
>>>soup.div.text
你好,世界
>>>soup.div.find('text')
你好,世界!

谢谢,不知怎么的,你完全忘记了这个函数。已经用过了,正如你回答的那样。
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''
... <div><text>Hello world!</text></div>''')
>>> soup.div.text
u'Hello world!'
>>> soup.div.find('text')
<text>Hello world!</text>