2012-10-08 5 views

Répondre

3

J'utiliser xml.etree.ElementTree:

def get_text(etree): 
    for child in etree: 
     if child.text: 
      yield child.text 
     if child.tail: 
      yield child.tail 

import xml.etree.ElementTree as ET 
root = ET.fromstring('<html> <h1> good morning </h1> welcome </html>') 
print list(get_text(root)) 
0

J'utiliser la bibliothèque python Beautiful Soup pour atteindre votre objectif. C'est juste un couple de lignes avec son aide:

from bs4 import BeautifulSoup 
soup = BeautifulSoup('<html> <h1> good morning </h1> welcome </html>') 
print [text for text in soup.stripped_strings] 
Questions connexes