2016-04-16 16 views
1

は:抽出内容は、私は、このHTMLデータからすべての<code>td</code>情報を取得するにはどうすればよい

response =requests.get(url) 
soup = BeautifulSoup(response.text, 'html.parser') 
table = soup.findAll('table', attrs={'class': 'StaffList'}) 

list_of_rows = [] 
for row in table.findAll('tr'): #2 rows found in table -loop through 
    list_of_cells = [] 
    for cell in row.findAll('td'): # each cell in in a row 
     text = cell.text.replace('&nbsp','') 
     list_of_cells.append(text) 
    #print list_of_cells 
    list_of_rows.append(list_of_cells) 
#print all cells in the two rows 
print list_of_rows 

エラーメッセージ:

<h1>All staff</h1> 
<h2>Manager</h2> 
<table class="StaffList"> 
    <tbody> 
     <tr> 
      <th>Name</th> 
      <th>Post title</th> 
      <th>Telephone</th> 
      <th>Email</th> 
     </tr> 
     <tr> 
      <td> 
       <a href="http://profiles.strx.usc.com/Profile.aspx?Id=Jon.Staut">Jon Staut</a> 
      </td> 
      <td>Line Manager</td> 
      <td>0160 315 3832</td> 
      <td> 
       <a href="mailto:[email protected]">[email protected]</a> &nbsp;</td> 
     </tr> 
    </tbody> 
</table> 
<h2>Junior Staff</h2> 
<table class="StaffList"> 
    <tbody> 
     <tr> 
      <th>Name</th> 
      <th>Post title</th> 
      <th>Telephone</th> 
      <th>Email</th> 
     </tr> 
     <tr> 
      <td> 
       <a href="http://profiles.strx.usc.com/Profile.aspx?Id=Peter.Boone">Peter Boone</a> 
      </td> 
      <td>Mailer</td> 
      <td>0160 315 3834</td> 
      <td> 
       <a href="mailto:[email protected]">[email protected]&nbsp;</a> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <a href="http://profiles.strx.usc.com/Profile.aspx?Id=John.Peters">John Peters</a> 
      </td> 
      <td>Builder</td> 
      <td>0160 315 3837</td> 
      <td> 
       <a href="mailto:[email protected]">[email protected].com</a> 
      </td> 
     </tr> 
    </tbody> 
</table> 

ここでエラーが発生した私のコードです

AttributeError: 'ResultSet' object has no attribute 'findAll' 

コードで2つのWebタグのすべての情報を出力するには、何が必要ですかベルス?

+0

「soup.findAll( 'td') 'を直接試してみませんか? – estebanpdl

答えて

2

問題は、この行から始まる:

table = soup.findAll('table', attrs={'class': 'StaffList'}) 

findAllには属性findAllを持っていない配列を返します。 テーブル= soup.find( 'テーブル'、ATTRS = { 'クラス': 'StaffList'})

単に、findAllfindへの変更

1

あるいは、を返すためにCSSセレクタ式を使用することができtable最初を抽出することなくStaffListテーブルからtr要素:提案の男性のための

for row in soup.select('table.StaffList tr'): #2 rows found in table -loop through 
    ...... 
0

感謝。で置換

for row in table.findAll('tr'): 

最初のもの:

table = soup.findAll('tr') 

第二1:

table = soup.findAll('table', attrs={'class': 'StaffList'}) 

に置き換え問題は、現在のコードの2行を交換した後解決

for row in table: 
関連する問題