2015-06-01 9 views
5

Oto co mam do tej pory:Usuń wszystkie style, skrypty i tagi HTML stronie html

from bs4 import BeautifulSoup 

def cleanme(html): 
    soup = BeautifulSoup(html) # create a new bs4 object from the html data loaded 
    for script in soup(["script"]): 
     script.extract() 
    text = soup.get_text() 
    return text 
testhtml = "<!DOCTYPE HTML>\n<head>\n<title>THIS IS AN EXAMPLE </title><style>.call {font-family:Arial;}</style><script>getit</script><body>I need this text captured<h1>And this</h1></body>" 

cleaned = cleanme(testhtml) 
print (cleaned) 

To działa w celu usunięcia skryptu

+1

Jaki jest Twój oczekiwany wynik? –

Odpowiedz

5

Wygląda na to prawie mam. Musisz również usunąć znaczniki html i kod stylizacji css. Oto moje rozwiązanie (I aktualizowany funkcji):

def cleanMe(html): 
    soup = BeautifulSoup(html) # create a new bs4 object from the html data loaded 
    for script in soup(["script", "style"]): # remove all javascript and stylesheet code 
     script.extract() 
    # get text 
    text = soup.get_text() 
    # break into lines and remove leading and trailing space on each 
    lines = (line.strip() for line in text.splitlines()) 
    # break multi-headlines into a line each 
    chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) 
    # drop blank lines 
    text = '\n'.join(chunk for chunk in chunks if chunk) 
    return text 
1

Jeśli chcesz szybki i brudny roztwór ca użyć:

re.sub(r'<[^>]*?>', '', value) 

Aby równowartość strip_tags w PHP. Czy tego chcesz?

7

Możesz użyć decompose, aby całkowicie usunąć znaczniki z dokumentu i generatora stripped_strings, aby pobrać zawartość znacznika.

def clean_me(html): 
    soup = BeautifulSoup(html) 
    for s in soup(['script', 'style']): 
     s.decompose() 
    return ' '.join(soup.stripped_strings) 

>>> clean_me(testhtml) 
'THIS IS AN EXAMPLE I need this text captured And this' 
Powiązane problemy