2015-04-22 13 views

risposta

1

ho usato il metodo str.replace per questo:

>>> li = soup.find('li') # or however you need to drill down to the <li> tag 
>>> mytext = li.text.replace(li.find('span').text, "") 
>>> print mytext 
Serve Returned 
1
import bs4 
html = r"<li> <span> 929</span> Serve Returned </li>" 
soup = bs4.BeautifulSoup(html) 
print soup.li.findAll(text=True, recursive=False) 

Questo dà:

[u' ', u' Serve Returned '] 

Il primo elemento è il "testo" che hai prima campata. Questo metodo potrebbe aiutarti a trovare il testo prima e dopo (e in mezzo) qualsiasi elemento figlio.

Problemi correlati