python - 选择带有美丽汤的嵌套元素

标签 python html beautifulsoup

我有以下 html:

<div class="leftColumn">
  <div>
     <div class="static">
     text1
     <br>
     text2
     <br>
     (222) 123 - 4567
     <br>
     <div class="summary">

如何使用 beautiful soup 仅选择文本行。

我尝试过多种方法,例如:

soup.select('.leftColumn div').text

但到目前为止还没有骰子

最佳答案

Mauro 的答案可能更符合您的需求,但这是另一种方法,也是我如何考虑获取内部 div 文本的:

from bs4 import BeautifulSoup
html = '''<div class="leftColumn">
  <div>
     <div class="static">
     text1
     <br>
     text2
     <br>
     (222) 123 - 4567
     <br>
     <div class="summary">
     '''
bs = BeautifulSoup(html)
for div in bs.findAll('div', attrs={'class': 'leftColumn'}):
    print div.findNext('div').findNext('div').text

关于python - 选择带有美丽汤的嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25146415/

相关文章:

python - "unfair" Pandas 分类.from_codes

html - 为什么我的网站在移动 View 中有水平滚动条?

html - 响应高度与宽度成正比

python - Beautifulsoup 未返回页面的完整 HTML

html - 从 Kickstarter 项目中抓取文本不会返回任何结果

python - 我的 ping 扫描程序无法运行。我究竟做错了什么?

python - 什么是多维字典?

python - "is"关键字可能等同于 Python 中的相等运算符的类型

javascript - d3.js 以 HTML 为节点的二叉树

python - 奇怪的 Pandas.read_html 错误