【问题标题】:How to extract nested level of a HTML element using BeautifulSoup?如何使用 BeautifulSoup 提取 HTML 元素的嵌套级别?
【发布时间】:2012-11-21 18:23:34
【问题描述】:

我目前正在使用 BeautifulSoup 来提取 HTML 元素和属性。
我也想知道提取的每个元素的嵌套级别。

例如:

示例 HTML:

<html>
  <head>
    <title>Element Attributes Test</title>
  </head>
  <body>
    <div id="abc">
      <ol id="def">
        <li class="testItem"> <a href="http://testpage.html">
        </li>
        <li class="testItem"> <table id="testTable">
               <tr>
                 <td>
                   <div id="testDiv">
                   </div>
                 </td>
               </tr>
             </table>
        </li>
      </ol>
    </div>  
  </body>  
</html>

我想获取特定元素的路径信息作为路径列中的输出。

----------------------------------
Element | Attribute | Path
----------------------------------
html    | None      | document   
----------------------------------
head    | None      | html
----------------------------------
title   | None      | html.head
----------------------------------
body    | None      | html
----------------------------------
div     | id="abc"  | html.body
-----------------------------------
ol      | id="def"  | html.body.div
-----------------------------------
li      | class=".."| html.body.div.ol
-----------------------------------
a       | href=".." | html.body.div.ol.li
-----------------------------------
li      | class=".."| html.body.div.ol
-----------------------------------
table   | id="..."  | html.body.div.old.li
-----------------------------------
tr      | None      | html.body.div.li.table
-----------------------------------

我能够提取 Element 及其相关属性,但无法找到合适的方法来获取该特定元素的路径。

如何使用 BeautifulSoup 提取相同的内容? 是否有任何其他库可以用来提取相同的内容?

提前致谢。

【问题讨论】:

    标签: python beautifulsoup lxml


    【解决方案1】:

    您可以采用以下方法为所有 html 元素获取自下而上的路径

    >>> for elem in soup.findAll():
        path = '.'.join(reversed([p.name for p in elem.parentGenerator() if p]))
        print "{:10}|{:60}|{:10}".format(elem.name,elem.attrs, path)
    
    
    html      |[]                                                          |[document]
    head      |[]                                                          |[document].html
    title     |[]                                                          |[document].html.head
    body      |[]                                                          |[document].html
    div       |[(u'id', u'abc')]                                           |[document].html.body
    ol        |[(u'id', u'def')]                                           |[document].html.body.div
    li        |[(u'class', u'testItem')]                                   |[document].html.body.div.ol
    a         |[(u'href', u'http://testpage.html')]                        |[document].html.body.div.ol.li
    li        |[(u'class', u'testItem')]                                   |[document].html.body.div.ol
    table     |[(u'id', u'testTable')]                                     |[document].html.body.div.ol.li
    tr        |[]                                                          |[document].html.body.div.ol.li.table
    td        |[]                                                          |[document].html.body.div.ol.li.table.tr
    div       |[(u'id', u'testDiv')]                                       |[document].html.body.div.ol.li.table.tr.td
    >>> 
    

    【讨论】:

      【解决方案2】:

      要获取路径,您可以尝试以下操作:

      [i.name for i in soup.findAll('div',{'id':'testDiv'})[0].findParents()]
      

      输出:

      ['td', 'tr', 'table', 'li', 'ol', 'div', 'body', 'html', u'[document]']
      

      或:

       '.'.join([i.name for i in soup.findAll('div',{'id':'testDiv'})[0].findParents()][::-1])
      

      将其作为字符串获取:

      u'[document].html.body.div.ol.li.table.tr.td'
      

      【讨论】:

        猜你喜欢
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 2022-01-01
        • 2013-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多