2017-07-19 19 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

2行目からのコメント(これらの行は<!-- -->)であり、通常のマークアップ要素ではないため、エラーが発生します。彼らは通常BeautifulSoupの方法で捕まえられません。そのため、あなたのrows要素が空です。

コメントにアクセスするにはCommentオブジェクトをbs4から使用する必要があります。 私も同様の質問に答えました: Accessing commented HTML Lines with BeautifulSoup