如何使用BeautifulSoup查找节点的子节点 [英] How to find children of nodes using BeautifulSoup
本文介绍了如何使用BeautifulSoup查找节点的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取所有<a>
标记,这些标记是<li>
的子代:
I want to get all the <a>
tags which are children of <li>
:
<div>
<li class="test">
<a>link1</a>
<ul>
<li>
<a>link2</a>
</li>
</ul>
</li>
</div>
我知道如何找到像这样的特定类的元素:
I know how to find element with particular class like this:
soup.find("li", { "class" : "test" })
但是我不知道如何找到所有<a>
属于<li class=test>
的子代,但是找不到其他任何子代.
But I don't know how to find all <a>
which are children of <li class=test>
but not any others.
就像我要选择的一样:
<a>link1</a>
推荐答案
尝试一下
li = soup.find('li', {'class': 'text'})
children = li.findChildren("a" , recursive=False)
for child in children:
print child
这篇关于如何使用BeautifulSoup查找节点的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文