HTML 遍历

it2022-05-05  84

HTML 遍历

HTML基本格式:

1.下行遍历:

属性说明contents子节点的列表,将所有儿子节点存入列表children子节点的迭代类型,与.contents类似,用于循环遍历儿子节点descendants子孙节点的迭代类型,包含所有子孙节点,用于循环遍历 ##遍历儿子结点 for child in soup.body.children: print(child) ##遍历子孙结点 for child in soup.body.descendants: print(child)

2.上行遍历:

属性说明parent结点的父亲标签parents节点先辈标签的迭代类型,用于循环遍历先辈节点 ##遍历父辈结点 for parent in soup.a.parents: if parent is None: print(parent) else: print(parent.name)

3. 平行遍历

属性说明next_sibling返回按照HTML文本顺序的下一个平行节点标签previous_sibling返回按照HTML文本顺序的上一个平行节点标签next_siblings迭代类型,返回按照HTML文本顺序的后续所有平行节点标签previous_siblings迭代类型,返回按照HTML文本顺序的前续所有平行节点标签

注: 平行遍历发生在同一个父节点下的各节点间

##遍历后续结点 for sibling in soup.a.next_siblings: print(sibling) ##遍历前续结点 for sibling in soup.a.previous_siblings: print(sibling)

转载于:https://www.cnblogs.com/machine-lyc/p/10264022.html

相关资源:html遍历文件夹图片文件ie

最新回复(0)