是否可以在两个标签之间获取文本,而在其他标签之间?
例如:
<ul>
<span class = one > Text that I want </span>
<span class = two > Text that I want </span>
<li class = .... > Text that I want </li>
</ul>
所以。我只是想要标签<ul>
和</ul>
答案 0 :(得分:2)
尝试一下
In [31]: from bs4 import BeautifulSoup
In [32]: x = """<ul>
...: <span class = one > 1Text that I want </span>
...: <span class = two > 2Text that I want </span>
...: <li class = .... > 3Text that I want </li>
...: </ul>"""
In [33]: soup = BeautifulSoup(x)
In [34]: for li in soup.findAll('ul'):
...: print(li.text)
...:
1Text that I want
2Text that I want
3Text that I want
In [35]: