BeautfilSoup Python,获取两个标签之间的所有文本

时间:2019-09-16 11:58:19

标签: python html beautifulsoup

是否可以在两个标签之间获取文本,而在其他标签之间?

例如:

<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>

之间的任何字符串

1 个答案:

答案 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]: