【问题标题】:how to get text in nested div with beautifulsoup如何使用 Beautifulsoup 在嵌套 div 中获取文本
【发布时间】:2017-02-11 12:48:13
【问题描述】:

嵌套的 div

<div class="a">
    <div class="b">first part text</div>
    second part text
</div>

这是我的代码

infos = soup.findAll("div", {"class": "a"})
print(infos[0].getText())

控制台打印是first part textsecond part text 两者。如何仅提取second part text

感谢您的回答。

【问题讨论】:

    标签: python css beautifulsoup lxml


    【解决方案1】:
    1. 使用find()返回find_all()中的第一个元素
    2. 使用. 查找infos 标记中的下一个div 并使用next_sibling 查找文本

    infos = soup.find("div", {"class": "a"})
    print(infos.div.next_sibling)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2019-04-10
      • 2021-08-16
      • 2020-01-03
      相关资源
      最近更新 更多