2016-04-16 14 views
1

Wie kann ich alle td Informationen aus diesen HTML-Daten abrufen:Extrahieren von Inhalt von zwei Tabellen aus Web-Daten

<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]</a> 
      </td> 
     </tr> 
    </tbody> 
</table> 

Hier ist mein Code, der einen Fehler erzeugt:

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 

Fehlermeldung:

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

Was muss ich tun, um den Code alle Informationen in den beiden Web-ta ausgeben zu lassen Bles?

+0

Warum versuchst du nicht direkt 'sup.findAll ('td')'? – estebanpdl

Antwort

2

Das Problem beginnt bei dieser Zeile:

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

Die findAll ein Array zurückgibt, das kein findAll Attribut hat.

einfach, ändern Sie die findAll zu find: table = soup.find ('table', attrs = { 'Klasse': 'StaffList'})

1

Alternativ können Sie CSS-Selektor-Ausdruck verwenden zurückzukehren tr Elemente aus der StaffList Tabelle ohne die table ersten extrahieren zu müssen:

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

Dank für Anregungen Jungs. Problem nun gelöst nach dem Ersetzen von 2 Codezeilen:

Der erste:

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

ersetzt durch:

table = soup.findAll('tr') 

der zweiten:

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

ersetzt mit:

for row in table: