Parsing HTML Tabelle mit BeatifulSoup
Verfasst: Freitag 29. November 2013, 07:08
Hi,
ich habe folgendes HTML document
und aus dieser Tabell wuerde ich gerne die Werte "Count" und "Precentage" fuer T1 bis T6 extrahiren. Leider bin ich nicht in der lage auf die Werte zuzugreifen mit diesem code:
Ich bekomme diesen Error:
Wie ist es moeglich auf die Werte aus der Tabelle zuzugreifen?
Vielen Dank im Vorraus.
ich habe folgendes HTML document
Code: Alles auswählen
<html><head>
<meta charset="utf-8">
</head>
<body>
<a name="Test1">
<center>
<b>Test 1</b> <table border="0">
<tbody><tr>
<th> Type </th>
<th> Region </th>
</tr>
<tr>
<td> <table border="0">
<thead>
<tr>
<th><b>Type</b></th>
<th> </th>
<th> Count </th>
<th> Percent </th>
</tr>
</thead>
<tbody><tr>
<td> <b>T1</b> </td>
<th> </th>
<td class="numeric" bgcolor="#ff0000"> 34,314 </td>
<td class="numeric" bgcolor="#ff0000"> 31.648% </td>
</tr>
<tr>
<td> <b>T2</b> </td>
<th> </th>
<td class="numeric" bgcolor="#bf3f00"> 25,820 </td>
<td class="numeric" bgcolor="#bf3f00"> 23.814% </td>
</tr>
<tr>
<td> <b>T3</b> </td>
<th> </th>
<td class="numeric" bgcolor="#24da00"> 4,871 </td>
<td class="numeric" bgcolor="#24da00"> 4.493% </td>
</tr>
</tbody></table><br>
</td>
<td> <table border="0">
<thead>
<tr>
<th><b> Type</b></th>
<th> </th>
<th> Count </th>
<th> Percent </th>
</tr>
</thead>
<tbody><tr>
<td> <b>T4</b> </td>
<th> </th>
<td class="numeric" bgcolor="#ff0000"> 34,314 </td>
<td class="numeric" bgcolor="#ff0000"> 31.648% </td>
</tr>
<tr>
<td> <b>T5</b> </td>
<th> </th>
<td class="numeric" bgcolor="#53ab00"> 11,187 </td>
<td class="numeric" bgcolor="#53ab00"> 10.318% </td>
</tr>
<tr>
<td> <b>T6</b> </td>
<th> </th>
<td class="numeric" bgcolor="#bf3f00"> 25,820 </td>
<td class="numeric" bgcolor="#bf3f00"> 23.814% </td>
</tr>
</tbody></table><br>
</td>
</tr>
</tbody></table>
</center>
</a>
</body></html>
Code: Alles auswählen
from bs4 import BeautifulSoup
with open("/home/mit/tmp/test3.html") as f:
soup = BeautifulSoup(f, "lxml")
print soup.find_all('a', {'name':'Test1'})
for tr in soup.find_all('a', attrs={'name':'Test1'}).find_all('tr')[2:]:
tds = tr.find_all('td')
print tds
Code: Alles auswählen
Traceback (most recent call last):
File "test.py", line 8, in <module>
for tr in soup.find_all('a', attrs={'name':'Test1'}).find_all('tr')[2:]:
AttributeError: 'ResultSet' object has no attribute 'find_all'
Vielen Dank im Vorraus.