简体   繁体   English

python,创建嵌套的 html 节点。 我如何从图中遍历 Dom 树并找到它的直接子级并从中创建 html 文件?

[英]python, create nested html nodes. How do i traverse the Dom tree from a graph and find it's immediate children and create an html file from it?

I'm trying to recursively extract the DOM tree saved in the graph Database to granularly rewrite them as either HTML, JSON or other template files.我正在尝试递归提取保存在图形数据库中的 DOM 树,以将它们精细地重写为 HTML、JSON 或其他模板文件。 The motivation I needed to have an easy way of extracting and controlling DOM nodes, rewriting them into any needed templating format which can be integrated into CMS or MVC development where variable interpolation is paramount.我需要一种简单的方法来提取和控制 DOM 节点,将它们重写为任何需要的模板格式,这种格式可以集成到变量插值至关重要的 CMS 或 MVC 开发中。

My attempt using a recursion fails because I'm confused about what to use as the next node parent or child.我使用递归的尝试失败了,因为我对使用什么作为下一个节点父节点或子节点感到困惑。

What I need to do is visit having the following tree, I need to create an equivalent HTML file using for now either beautifulsoup or other utility.我需要做的是访问具有以下树,我需要创建一个等效的 HTML 文件,现在使用 beautifulsoup 或其他实用程序。

   html
        head
            title
        body
            div
            div
                ul
                    li

With the following code, without a recursive function, I'm able to get the immediate children resulting in the value of:使用以下代码,如果没有递归 function,我可以得到直接的孩子,从而产生以下值:

|> parent Id: 844424930131969 --- parent tag HTML
    child id 844424930131970 child tag: head
    child id 844424930131973 child tag: body

or the following html file:或以下 html 文件:

<html>
 <head>
 </head>
 <body>
 </body>
</html>

How do I pass d to be able to be part of the next recursion where I can obtain its children?我如何通过d才能成为下一个递归的一部分,在那里我可以获得它的孩子?

cursor = ag.execCypher("MATCH (n:node {tag: 'html'}) RETURN n")    
t = [x[0].id for x in cursor]
print(t[0])

def graph_dom(t_id):
    parent = ag.execCypher("MATCH (n:node) WHERE id(n) = %s RETURN n", params=(t_id,))
    p = [x[0] for x in parent]
    pt = p[0]["tag"]       
    pid = p[0].id
    print(f"|> parent Id: {pid} --- parent tag {pt}")
    parent_tag = soup.new_tag(name=p[0]["tag"])
    soup.append(parent_tag)
    children = ag.execCypher("MATCH (v:node)-[R:connect]->(V2) WHERE id(v) = %s RETURN V2", params=(p[0].id,))
    for d in children:
        children_tag = soup.new_tag(name=d[0]["tag"])
        parent_tag.append(children_tag)
        dt = d[0]["tag"]
        did = d[0].id
        print(f"child id {did} child tag: {dt}")        
    # graph_dom()   # I need to add a recursive argument   
        
graph_dom(t[0])

file_soup = soup.prettify()
with open("helloworld.html", "w") as file:
    file.write(str(file_soup))

I think you should call graph_dom(did) recursively in the children loop.我认为您应该在子循环中递归调用 graph_dom(did) 。

cursor = ag.execCypher("MATCH (n:node {tag: 'html'}) RETURN n")    
t = [x[0].id for x in cursor]
print(t[0])

def graph_dom(t_id):
    parent = ag.execCypher("MATCH (n:node) WHERE id(n) = %s RETURN n", params=(t_id,))
    p = [x[0] for x in parent]
    pt = p[0]["tag"]       
    pid = p[0].id
    print(f"|> parent Id: {pid} --- parent tag {pt}")
    parent_tag = soup.new_tag(name=p[0]["tag"])
    soup.append(parent_tag)
    children = ag.execCypher("MATCH (v:node)-[R:connect]->(V2) WHERE id(v) = %s RETURN V2", params=(p[0].id,))
    for d in children:
        children_tag = soup.new_tag(name=d[0]["tag"])
        parent_tag.append(children_tag)
        dt = d[0]["tag"]
        did = d[0].id
        print(f"child id {did} child tag: {dt}")        
        graph_dom(did)   # Call graph_dom recursively.   
        
graph_dom(t[0])

file_soup = soup.prettify()
with open("helloworld.html", "w") as file:
    file.write(str(file_soup))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM