python - 如何访问 BeautifulSoup 中名为 "name"的标签

标签 python xml beautifulsoup

我想访问一个名为 "name" 的标签,例如:

<contact><name>Yesügey</name><lastName>Yeşil</lastName><phone>+90 333 9695395</phone></contact>

由于 "name" 是 BeautifulSoup 标签对象的属性,我无法访问子标签 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>

最佳答案

你可以这样试试,

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

或者

print soup.find('name').string

关于python - 如何访问 BeautifulSoup 中名为 "name"的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14435268/

相关文章:

Python 类变量更改未保存

java - 如何使用 Retrofit 库解析 RSS xml 提要

Java 对象分配开销

python - 尝试使用 BeautifulSoup 从网页中获取绝对链接

python - 使用 Python 和 BeautifulSoup 解析表

python - 在python中查找邻居邻居的最有效方法

python - 测试由于浮点限制而导致的舍入误差

java - 使用 XStream 和 JsonHierarchicalStreamDriver 输出值,如何舍入 double ?

python - 如何使用 BeautifulSoup 模拟 ":contains"?

python - 如何解决列表索引超出范围错误?