如何使用BeautifulSoup从HTML页面获得树状结构?

2024-04-25 01:47:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一些代码来解析HTML页面,使用beautiful soup得到一个树状结构。树状结构是使用名为Item的数据类型及其子属性.child()实现的。生成的树状结构重复了孩子们好几次,所以我不确定问题到底出在哪里

def Parse(self,i):

        if(not None):

             child=[]
             content=""
             if(i.contents is not None):


                 for k in i.contents:

                    #print(k)
                    if(type(k)==bs4.element.NavigableString):

                        content+=k

                    elif(type(k)==bs4.element.Tag):

                        print(k.siblings)
                        child.append(self.Parse(k))

             h=Item(i.name,content=content, attributes=i.attrs)
             for t in child:

                 h.add(t)

             return h

Tags: inselfnonechildforifparsetype