7

Vorrei raschiare un tavolo dal web e mantenere il & nbsp; entità intatte in modo che io possa ripubblicare come HTML in seguito. Tuttavia, BeautifulSoup sembra convertirli in spazi. Esempio:Raschiare utilizzando Beautiful Soup preserving   entità

from bs4 import BeautifulSoup 

html = "<html><body><table><tr>" 
html += "<td>&nbsp;hello&nbsp;</td>" 
html += "</tr></table></body></html>" 

soup = BeautifulSoup(html) 
table = soup.find_all('table')[0] 
row = table.find_all('tr')[0] 
cell = row.find_all('td')[0] 

print cell 

risultato osservato:

<td> hello </td> 

risultato richiesto:

<td>&nbsp;hello&nbsp;</td> 

risposta

5

In BS4 convertEntities parametro al costruttore BeautifulSoup non è più supportato. Le entità HTML vengono sempre convertite nei corrispondenti caratteri Unicode (vedere docs).

Secondo documenti, è necessario utilizzare un formattatore di uscita, in questo modo:

print soup.find_all('td')[0].prettify(formatter="html") 
+0

Grazie per la risposta :) –

Problemi correlati