提问者:小点点

所有HTML节点到XPATH


我正在尝试将所有超文本标记语言节点转换为XPATH这是一个示例输入。基于超文本标记语言,我正在寻找所有子节点的所有XPATH

<html>
    <head>
        <title>
            The Dormouse's story
        </title>
    </head>
    <body>
        <p class="title">
            <b>
                The Dormouse's story
            </b>
        </p>
        <span>Hello</span>
    </body>
</html>

我想要的输出

html
html/head
html/head/title
html/body 
html/body/p

我目前拥有的

{
    "name": "[document]",
    "attr": {},
    "children": [
        {
            "name": "html",
            "attr": {},
            "children": [
                {
                    "name": "head",
                    "attr": {},
                    "children": [
                        {
                            "name": "title",
                            "attr": {},
                            "children": []
                        }
                    ]
                },
                {
                    "name": "body",
                    "attr": {},
                    "children": [
                        {
                            "name": "p",
                            "attr": {
                                "class": [
                                    "title"
                                ]
                            },
                            "children": [
                                {
                                    "name": "b",
                                    "attr": {},
                                    "children": []
                                }
                            ]
                        },
                        {
                            "name": "span",
                            "attr": {},
                            "children": []
                        }
                    ]
                }
            ]
        }
    ]
}

代码

try:
    import os
    import lxml.etree
    from bs4 import BeautifulSoup
    import json
    import etree
except Exception as e:
    pass

def traverse(soup):

    if soup.name is not None:
        dom_dictionary = {}
        dom_dictionary['name'] = soup.name
        dom_dictionary['attr'] = soup.attrs

        dom_dictionary['children'] = [
            traverse(child)
            for child in soup.children if child.name is not None
        ]

        return dom_dictionary

with open("html.txt", "r") as f:
    data = f.read()
    soup = BeautifulSoup(data, 'html.parser')
    JsonDom = traverse(soup)
    print(json.dumps(JsonDom, indent=4))


如果你们能给我指出正确的方向,任何帮助都会很好

我确实研究了Lxml bs4和硒,但不幸的是没有运气


共1个答案

匿名用户

html_doc = """
<html>
    <head>
        <title>
            The Dormouse's story
        </title>
    </head>
    <body>
        <p class="title">
            <b>
                The Dormouse's story
            </b>
        </p>
        <span>Hello</span>
    </body>
</html>
"""


def generate(soup, cur=""):
    for tag in soup.find_all(recursive=False):
        yield cur + tag.name
        yield from generate(tag, cur=cur + tag.name + "/")


soup = BeautifulSoup(html_doc, "html.parser")  # you can also use  "lxml" or "html5lib"
for t in generate(soup):
    print(t)

打印:

html
html/head
html/head/title
html/body
html/body/p
html/body/p/b
html/body/span

相关问题