小编GKE*_*GKE的帖子

使用BeautifulSoup 4和递归来捕获HTML嵌套标签的结构

我一直在尝试使用BeautifulSoup4和递归来捕获HTML页面的布局.我们的想法是将父项的数据结构链接到子项,例如,这样的布局:

<html>
 <h1>
  <!--Contents-->
 </h1>
 <div> 
  <div> 
   <!--Contents-->
  </div>
 </div>
</html>
Run Code Online (Sandbox Code Playgroud)

将存储在如下列表中:

html = [ h1 , div ] # Where h1 and div are also lists also containing lists
Run Code Online (Sandbox Code Playgroud)

我很难找到关于这个特定问题的Q&A,所以我尝试使用递归遍历目录来建模函数,因为它们非常相似.

这是我在Python 3中的当前函数,它应该将标记嵌入到列表中:

def listGen(l , hObj):
    # Where hObj is a BS4 object and l is a sorted lists containing direct children to the html tag
    z = []
    for x in l:
        z.append(hObj.find(x).children)

    def expand(xlist1):
        # Where xlist1 is a list generator 
        for n in xlist1: …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup python-3.x

5
推荐指数
1
解决办法
289
查看次数

标签 统计

beautifulsoup ×1

python ×1

python-3.x ×1