Domanda

C'è un modo per dividere una lunga serie di HTML dopo N parole? Ovviamente ho potuto utilizzare:

' '.join(foo.split(' ')[:n])

per ottenere le prime n parole di una stringa di testo semplice, ma che potrebbero dividere nel mezzo di un tag html, e non produrrà Valid HTML perché non chiuderà i tag che sono state aperte.

ho bisogno di fare questo in un sito Zope / Plone - se c'è qualcosa di serie in quei prodotti che possono farlo, che sarebbe l'ideale

.

Ad esempio, dire che ho il testo:

<p>This is some text with a 
  <a href="http://www.example.com/" title="Example link">
     bit of linked text in it
  </a>.
</p>

E lo chiedo per dividere dopo 5 parole, dovrebbe restituire:

<p>This is some text with</p>

7 parole:

<p>This is some text with a 
  <a href="http://www.example.com/" title="Example link">
     bit
  </a>
</p>
È stato utile?

Soluzione

Date un'occhiata alla truncate_html_words funzione django.utils.text. Anche se non si utilizza Django, il codice non fa esattamente quello che vuoi.

Altri suggerimenti

Ho sentito dire che Beautiful Soup è molto bravo a parsing del codice HTML. Probabilmente sarà in grado di aiutarvi a ottenere HTML corretto fuori.

Stavo per accennare alla base HTMLParser che è costruito in Python, dato che non sono sicuro di quello che il risultato finale vostra cercando di raggiungere è, si può o non può arrivare lì, si lavorerà con i gestori in primo luogo

È possibile utilizzare un mix di regex, BeautifulSoup o Tidy (io preferisco BeautifulSoup). L'idea è semplice - a nudo tutti i tag HTML prima. Trova l'ennesima parola (n = 7 qui), trovare il numero di volte in cui l'ennesima parola compare nella stringa fino al n parole -. Coz u sono alla ricerca solo per l'ultima occorrenza da utilizzare per troncamento

Ecco un pezzo di codice, anche se un po 'disordinato, ma funziona

import re
from BeautifulSoup import BeautifulSoup
import tidy

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

input_string='<p>This is some text with a <a href="http://www.example.com/" '\
    'title="Example link">bit of linked text in it</a></p>'

s=remove_html_tags(input_string).split(' ')[:7]

###required to ensure that only the last occurrence of the nth word is                                                                                      
#  taken into account for truncating.                                                                                                                       
#  coz if the nth word could be 'a'/'and'/'is'....etc                                                                                                       
#  which may occur multiple times within n words                                                                                                            
temp=input_string
k=s.count(s[-1])
i=1
j=0
while i<=k:
    j+=temp.find(s[-1])
    temp=temp[j+len(s[-1]):]
    i+=1
####                                                                                                                                                        
output_string=input_string[:j+len(s[-1])]

print "\nBeautifulSoup\n", BeautifulSoup(output_string)
print "\nTidy\n", tidy.parseString(output_string)

L'uscita è che u vuole

BeautifulSoup
<p>This is some text with a <a href="http://www.example.com/" title="Example link">bit</a></p>

Tidy
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 6 November 2007), see www.w3.org">
<title></title>
</head>
<body>
<p>This is some text with a <a href="http://www.example.com/"
title="Example link">bit</a></p>
</body>
</html>

Spero che questo aiuti

Modifica Un'espressione regolare meglio

`p = re.compile(r'<[^<]*?>')`
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top