Reputation: 1039
I´m trying to get the information from this table:
<table class="table4 table4-1 table4-1-1"><thead><tr><th class="estilo1">No</th><th class="estilo2">Si</th><!-- <th><div class="contenedor-vinculos6"><a title="Ver más " class="vinculo-interrogacion" href="#">Más información</a></div></th>--></tr></thead><tbody><tr><td class="estilo1"><span class="estilo3">100%<span class="numero-voto">(15)</span></span><div class="grafica1 grafica1-desacuerdo"><div class="item-grafica" style="width: 100%;"/></div></div></td><td class="estilo2"><span class="estilo3">0%<span class="numero-voto">(0)</span></span><div class="grafica1 grafica1-deacuerdo"><div class="item-grafica" style="width: 0%;"/></div></div></td><td><span class="display-none">Más información</span></td></tr></tbody></table>
I´m doing the following in python3:
req = Request('http://www.congresovisible.org/votaciones/10918/',headers=headers)
web_page = urlopen(req)
soup = BeautifulSoup(web_page.read(), 'html.parser')
table= soup.find_all('table', attrs={'class':'table4 table4-1 table4-1-1'})
This works but only shows part of the table, it excludes everything after:
<td class="estilo2"><span class="estilo3...)
This is the output
[<table class="table4 table4-1 table4-1-1"><thead><tr><th class="estilo1">No</th><th class="estilo2">Si</th><!-- <th><div class="contenedor-vinculos6"><a title="Ver más " class="vinculo-interrogacion" href="#">Más información</a></div></th>--></tr></thead><tbody><tr><td class="estilo1"><span class="estilo3">100%<span class="numero-voto">(15)</span></span><div class="grafica1 grafica1-desacuerdo"><div class="item-grafica" style="width: 100%;"></div></div></td></tr></tbody></table>]
How could I extract the whole table?
Upvotes: 1
Views: 1222
Reputation: 474251
It is actually quite easy to solve. html.parser
does not parse this kind of non-well-formed HTML well. Use a more lenient html5lib
instead. This works for me:
import requests
from bs4 import BeautifulSoup
response = requests.get('http://www.congresovisible.org/votaciones/10918/')
soup = BeautifulSoup(response.content, 'html5lib')
table = soup.find_all('table', attrs={'class':'table4 table4-1 table4-1-1'})
print(table)
Note that this requires html5lib
package to be installed:
pip install --upgrade html5lib
By the way, lxml
parser works as well:
soup = BeautifulSoup(response.content, 'lxml')
Upvotes: 1