【问题标题】: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?python,创建嵌套的 html 节点。如何从图中遍历 Dom 树并找到它的直接子级并从中创建一个 html 文件?
【发布时间】:2022-01-09 18:12:12
【问题描述】:

我正在尝试递归提取保存在图形数据库中的 DOM 树,以将它们精细地重写为 HTML、JSON 或其他模板文件。 我需要一种简单的方法来提取和控制 DOM 节点,将它们重写为任何需要的模板格式,这种格式可以集成到变量插值至关重要的 CMS 或 MVC 开发中。

我使用递归的尝试失败了,因为我对使用什么作为下一个节点父节点或子节点感到困惑。

我需要做的是访问 有了以下树,我现在需要使用 beautifulsoup 或其他实用程序创建一个等效的 HTML 文件。

   html
        head
            title
        body
            div
            div
                ul
                    li

使用下面的代码,没有递归函数,我可以得到直接的孩子,得到的值是:

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

或以下html文件:

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

我如何通过 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))

【问题讨论】:

    标签: python recursion beautifulsoup graph agens-graph


    【解决方案1】:

    我认为你应该在子循环中递归调用 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2016-03-13
      • 2023-03-17
      • 2017-09-18
      • 1970-01-01
      相关资源
      最近更新 更多