È possibile passare una funzione per find_all() per aiutarlo a verificare se la stringa è un commento.
Per esempio io ho seguito html:
<body>
<!-- Branding and main navigation -->
<div class="Branding">The Science & Safety Behind Your Favorite Products</div>
<div class="l-branding">
<p>Just a brand</p>
</div>
<!-- test comment here -->
<div class="block_content">
<a href="https://www.google.com">Google</a>
</div>
</body>
Codice:
from bs4 import BeautifulSoup as BS
from bs4 import Comment
....
soup=BS(html,'html.parser')
comments=soup.find_all(string=lambda text:isinstance(text,Comment))
for c in comments:
print c
print "==========="
c.decompose()
l'output sarà:
Branding and main navigation
============
test comment here
============
BTW, penso che il motivo per cui non lo fa find_all('Comment')
il lavoro è (dal documento BeautifulSoup):
Inserisci un valore per nome e dirai a Beautiful Soup di considerare solo i tag con determinati nomi. Le stringhe di testo verranno ignorate, così come i tag i cui nomi non corrispondono.
Questa [risposta] (http://stackoverflow.com/a/3507360/771848) dovrebbe ancora funzionare, suppongo. – alecxe
Ricevo che il "nome globale" commento "non è definito" – Joseph
Mi rendo conto che questo è vecchio, ma @Joseph, se si importa commento da bs4 dovrebbe funzionare – atarw