【发布时间】: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