2017-07-19 44 views
0

我想從一些HTML中使用python的BeautifulSoup提取一些信息。在HTML的BeautifulSoup findAll變量類型問題

Sudsection:

<div class="ui-grid-canvas"> 
          <!-- --> 
          <div class="ui-grid-row" ng-class="{'ui-grid-tree-header-row': row.treeLevel &gt; -1, 'ui-grid-row-dirty': row.isDirty, 'ui-grid-row-saving': row.isSaving, 'ui-grid-row-error': row.isError,'ui-grid-row-selected': row.isSelected}" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index" ng-style="Viewport.rowStyle(rowRenderIndex)"> 
           <div role="row" row-render-index="rowRenderIndex" ui-grid-row="row"> 
            <div role="row"> 
             <!-- --> 
             <div class="ui-grid-cell ui-grid-coluiGrid-0005" ng-class="{sorted: col.name==$parent.$parent.$parent.$parent.$parent.$parent.$parent.datatableImpl.sortedColumn}" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" role="gridcell" tabindex="0" ui-grid-cell=""> 
              <div class="ui-grid-cell-contents" ng-bind-html="row.entity[col.field].content" title="Alnwick-Haldimand">Alnwick-Haldimand</div> 
             </div> 
             <!-- --> 
             <div class="ui-grid-cell ui-grid-coluiGrid-0006" ng-class="{sorted: col.name==$parent.$parent.$parent.$parent.$parent.$parent.$parent.datatableImpl.sortedColumn}" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" role="gridcell" tabindex="0" ui-grid-cell=""> 
              <div class="ui-grid-cell-contents" ng-bind-html="row.entity[col.field].content" title="Alderville Community Centre">Alderville Community Centre</div> 
             </div> 
             <!-- --> 
             <div class="ui-grid-cell ui-grid-coluiGrid-0007" ng-class="{sorted: col.name==$parent.$parent.$parent.$parent.$parent.$parent.$parent.datatableImpl.sortedColumn}" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" role="gridcell" tabindex="0" ui-grid-cell=""> 
              <div class="ui-grid-cell-contents" ng-bind-html="row.entity[col.field].content" title="Under construction">Under construction</div> 
             </div> 
             <!-- --> 
             <div class="ui-grid-cell ui-grid-coluiGrid-0008" ng-class="{sorted: col.name==$parent.$parent.$parent.$parent.$parent.$parent.$parent.datatableImpl.sortedColumn}" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" role="gridcell" tabindex="0" ui-grid-cell=""> 
              <div class="ui-grid-cell-contents" ng-bind-html="row.entity[col.field].content" title="March 2018">March 2018</div> 
             </div> 
             <!-- --> 
            </div> 
           </div> 
           <!-- --> 
           <!-- --> 
          </div> 

我遇到一個奇怪的錯誤。下面是一個代碼塊的量,問題是發生:

table = page_soup.findAll('div',attrs={"class" : "ui-grid-canvas"}) 
print(type(table[0])) 

rows = table[0].findAll('div',attrs={"class": "ui-grid-row"}) 
print(type(rows[0])) 

cell = rows[0].findALL('div') 
print(type(cells)) 

這些線返回以下:

<class 'bs4.element.Tag'> 
<class 'bs4.element.Tag'> 

TypeError         Traceback (most recent call last) 

<ipython-input-56-13fce9e4b865> in <module>() 
     5 print(type(rows[0])) 
     6 
----> 7 cell = rows[0].findALL('div') 
     8 print(type(cells)) 

TypeError: 'NoneType' object is not callable 

爲什麼這個正上方上的變量類型檢查時返回一個錯誤類型表明它是在表變量的情況下工作的bs4.element.Tag?

使用Ubuntu,Python 3.6和BS4。

在此先感謝。

回答

1

發生錯誤是因爲從第二行開始註釋(包含這些行:<!-- -->),而不是普通的標記元素。他們通常不會被BeautifulSoup方法所捕獲。這就是您的rows元素爲空的原因。

您需要訪問的評論是從bs4使用Comment對象。 我已經在這裏回答了一個類似的問題: Accessing commented HTML Lines with BeautifulSoup