如何使用BeautifulSoup来获得深度嵌套的DIV值? [英] How can I use BeautifulSoup to get deeply nested div values?
本文介绍了如何使用BeautifulSoup来获得深度嵌套的DIV值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要得到的值深层嵌套<跨度>
在DOM结构,看起来像这样的元素:
I need to get the values of deeply nested <span>
elements in a DOM structure that looks like this:
<div class="panda">
<div class="that">
<ul class="foo">
<li class="bar">
<div class="hi">
<p class="bye">
<span class="cheese">Cheddar</span>
这个问题。
soup.findAll(跨越,{级:奶酪})
是,有数以百计的网页上跨度元素与一流的奶酪,所以我需要通过类熊猫来过滤。我需要得到值的列表如 [切德,巴马,瑞士]
is that there are hundreds of span elements on the page with class "cheese" so I need to filter them by class "panda". I need to get a list of values like ["Cheddar", "Parmesan", "Swiss"]
推荐答案
使用CSS选择器:
[e.get_text() for e in soup.select('.panda .cheese')]
或者,如果你preFER find_all
:
# Calling a soup or tag is the same as find_all
[e.get_text() for panda in soup('div', {'class': 'panda'})
for e in panda('span', {'class': 'cheese'})]
这篇关于如何使用BeautifulSoup来获得深度嵌套的DIV值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文