html代码:
<td class="_480u">
<div class="clearfix">
<div>
Female
</div>
</div>
</td>
我想要值“Female”作为输出。
我尝试了 bs.findAll('div',{'class':'clearfix'})
; bs.findAll('tag',{'class':'_480u'})
但是这些类遍布我的 html 代码,输出是一个大列表。我想在我的搜索中加入 {td --> class = ".."and div --> class = ".."},这样我得到的输出是 Female。我该怎么做?
谢谢
最佳答案
使用stripped_strings
属性:
>>> from bs4 import BeautifulSoup
>>>
>>> html = '''<td class="_480u">
... <div class="clearfix">
... <div>
... Female
... </div>
... </div>
... </td>'''
>>> soup = BeautifulSoup(html)
>>> print ' '.join(soup.find('div', {'class': 'clearfix'}).stripped_strings)
Female
>>> print ' '.join(soup.find('td', {'class': '_480u'}).stripped_strings)
Female
或将类指定为空字符串(或None
)并使用string
属性:
>>> soup.find('div', {'class': ''}).string
u'\n Female\n '
>>> soup.find('div', {'class': ''}).string.strip()
u'Female'
关于python - 如何使用没有类的 BeautifulSoup 提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18443694/