美文网首页
Beautiful Soup 关联选择中的父节点和祖先节点、兄弟

Beautiful Soup 关联选择中的父节点和祖先节点、兄弟

作者: 日落_3d9f | 来源:发表于2020-12-08 23:11 被阅读0次

转载自:https://blog.csdn.net/qq_42732153/article/details/81105725

Python 用Beautiful Soup解析选择的节点元素

子节点和子孙节点

html = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="story">
    Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">
<span>Elsie</span>
</a>
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>
and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.p.contents)
print(soup.p.children)
for i, child in enumerate(soup.p.children, start=1):
    print(i, child)

#所有的子孙节点
print(soup.p.descendants)
for i, child in enumerate(soup.p.descendants, start=1):
    print(i, child)

print(’----------------------------------------------‘—)

# 父节点和祖先节点
html = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="story">
            Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"
<span>Elsie</span>
</a>
</p>
<p class="story">...</p>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.a.parent)

# 所有的祖先节点
print("------------------------------------------------------")
html = """
<html>
<body>
<p class="story">
<a href="http://example.com/elsie" class="sister" id="link1">
<span>Elsie</span>
</a>
</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(type(soup.a.parents))
print(list(enumerate(soup.a.parents)))

# 兄弟节点
print("--------------------------------------------------------")
html = """
<html>
<body>
<p class="story">
            Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">
<span>Elsie</span>
</a>
            Hello
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>
            and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
            and they lived at the bottom of a well.
</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('Next Sibling', soup.a.next_sibling)
print('Prev Sibling', soup.a.previous_sibling)
print('Next Siblings', list(enumerate(soup.a.next_siblings)))
print('Prev Siblings', list(enumerate(soup.a.previous_siblings)))

# 提取信息
print('------------------------------------------------------------')
html = """
<html>
<body>
<p class="story">
            Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Bob</a><a href="http://example.com/locie"
class="sister" id="link2">Locie</a>
</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('Next Sibling:')
print(type(soup.a.next_sibling))
print(soup.a.next_sibling)
print(soup.a.next_sibling.string)
print('Parent')
print(type(soup.a.parents))
print(list(soup.a.parents)[0])
print(list(soup.a.parents)[0].attrs['class'])

相关文章

  • Beautiful Soup 关联选择中的父节点和祖先节点、兄弟

    转载自:https://blog.csdn.net/qq_42732153/article/details/811...

  • Python基础(47) - Beautiful Soup节点选

    如何使用Beautiful Soup的节点选择器获取节点信息 HTML代码如下: 安装beafutifulsoup...

  • ztree 勾选动作是否出发子或者父api

    父节点关联子节点,子节点取消时不关联父节点 设置chkboxType : { "Y" : "ps", "N" : ...

  • jQuery 常用的节点操作

    js 获取元素(父节点,子节点,兄弟节点) jQuery 获取元素(父节点,子节点,兄弟节点) 元素筛选

  • 4.树

    树的基本概念 节点、根节点、父节点、子节点、兄弟节点(同层节点不一定是兄弟节点,兄弟节点必须有同一个父节点) 空树...

  • JavaScript DOM常用笔记

    获取元素(父节点,子节点,兄弟节点)

  • 二叉树

    二叉树 概念 父节点、子节点 兄弟节点:具有相同父节点的子节点 根节点:没有父节点的节点 叶子节点(叶节点):没有...

  • 2001.树

    相关概念 根节点:没有父节点。 叶子节点:没有子节点。 父节点,子节点,兄弟节点。 三个比较相似的概念:高度(He...

  • 原生js的DOM相关操作

    获取节点 获取父节点: parentNode 获取兄弟节点:nextElementSibling || ne...

  • 获取节点

    1获取兄弟父节点的兄弟节点 this.praent.parentElement.parentNode.childr...

网友评论

      本文标题:Beautiful Soup 关联选择中的父节点和祖先节点、兄弟

      本文链接:https://www.haomeiwen.com/subject/vtlcgktx.html