[英]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?
我正在尝试递归提取保存在图形数据库中的 DOM 树,以将它们精细地重写为 HTML、JSON 或其他模板文件。 我需要一种简单的方法来提取和控制 DOM 节点,将它们重写为任何需要的模板格式,这种格式可以集成到变量插值至关重要的 CMS 或 MVC 开发中。
我使用递归的尝试失败了,因为我对使用什么作为下一个节点父节点或子节点感到困惑。
我需要做的是访问具有以下树,我需要创建一个等效的 HTML 文件,现在使用 beautifulsoup 或其他实用程序。
html
head
title
body
div
div
ul
li
使用以下代码,如果没有递归 function,我可以得到直接的孩子,从而产生以下值:
|> 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))
我认为您应该在子循环中递归调用 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.