python - BeautifulSoup:如何获取嵌套的 div

标签 python web-scraping beautifulsoup

给定以下代码:

<html>
<body>
<div class="category1" id="foo">
      <div class="category2" id="bar">
            <div class="category3">
            </div>
            <div class="category4">
                 <div class="category5"> test
                 </div>
            </div>
      </div>
</div>
</body>
</html>

如何提取单词test来自 <div class="category5"> test使用 BeautifulSoup 即如何处理嵌套的 div?我试图在 Internet 上查找,但没有找到任何处理易于掌握示例的案例,因此我设置了这个。谢谢。

最佳答案

xpath 应该是直接的答案,但是 BeautifulSoup 不支持。

更新:使用 BeautifulSoup 解决方案

为此,假设您知道本例中的 和元素 (div),您可以使用 for/loop使用 attrs 得到你想要的:

from bs4 import BeautifulSoup

html = '''
<html>
<body>
<div class="category1" id="foo">
      <div class="category2" id="bar">
            <div class="category3">
            </div>
            <div class="category4">
                 <div class="category5"> test
                 </div>
            </div>
      </div>
</div>
</body>
</html>'''

content = BeautifulSoup(html)

for div in content.findAll('div', attrs={'class':'category5'}):
    print div.text

test

我从你的 html 示例中提取文本没有问题,就像@MartijnPieters 建议的那样,你需要找出你的 div 元素丢失的原因。

另一个更新

由于您缺少 lxml 作为 BeautifulSoup 的解析器,这就是返回 None 的原因,因为您没有开始解析任何内容。安装 lxml 应该可以解决您的问题。

您可以考虑使用 lxml 或支持 xpath 的类似工具,如果您问我的话,这很简单。

from lxml import etree

tree = etree.fromstring(html) # or etree.parse from source
tree.xpath('.//div[@class="category5"]/text()')
[' test\n                 ']

关于python - BeautifulSoup:如何获取嵌套的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26627080/

相关文章:

Python Selenium 滚动到底部 Google Photos 相册

json - 如何使用querySelector进行网络抓取并在Flutter中的“文本”小部件中显示信息?

html - 从 HTML 中提取商品价格

python - 萨吉马斯的 BeautifulSoup4

python - 使用 requests.post 时 <a> 标签不显示 Href 属性

python - 保存时忽略内联模型

python - 当键值直接从字典分配给变量python时函数不起作用

python - Xpath - 在 xpath 结果中搜索

python - 将 href 字符串转换为链接列表

python - 在 python 中检查 lambda 中的断言