Come posso analizzare solo il testo da una pagina Web utilizzando jsoup utilizzando java?Come analizzare solo testo da HTML
risposta
Da jsoup libro di cucina: http://jsoup.org/cookbook/extracting-data/attributes-text-html
String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);
String text = doc.body().text(); // "An example link"
Bene, ecco un metodo rapido ho buttato insieme una volta. Usa le espressioni regolari per portare a termine il lavoro. La maggior parte delle persone concorderà sul fatto che questo non è un buon modo per farlo. Quindi, usare a proprio rischio.
public static String getPlainText(String html) {
String htmlBody = html.replaceAll("<hr>", ""); // one off for horizontal rule lines
String plainTextBody = htmlBody.replaceAll("<[^<>]+>([^<>]*)<[^<>]+>", "$1");
plainTextBody = plainTextBody.replaceAll("<br ?/>", "");
return decodeHtml(plainTextBody);
}
Questo è stato originariamente utilizzato nel mio API wrapper per le API Stack Overflow. Quindi, è stato testato solo sotto un piccolo sottoinsieme di tag html.
Hmmm ... perché non usi la semplice espressione regolare: 'replaceAll (" <[^>] +> "," ")'? – Crozin
@Crozin, beh, stavo insegnando a me stesso come usare i back-reference. Sembra che anche il tuo probabilmente funzionerebbe. – jjnguy
questo fa male! -> http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – sleeplessnerd
Utilizzo di classi che fanno parte del JDK:
import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
class GetHTMLText
{
public static void main(String[] args)
throws Exception
{
EditorKit kit = new HTMLEditorKit();
Document doc = kit.createDefaultDocument();
// The Document class does not yet handle charset's properly.
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
// Create a reader on the HTML content.
Reader rd = getReader(args[0]);
// Parse the HTML.
kit.read(rd, doc, 0);
// The HTML text is now stored in the document
System.out.println(doc.getText(0, doc.getLength()));
}
// Returns a reader on the HTML data. If 'uri' begins
// with "http:", it's treated as a URL; otherwise,
// it's assumed to be a local filename.
static Reader getReader(String uri)
throws IOException
{
// Retrieve from Internet.
if (uri.startsWith("http:"))
{
URLConnection conn = new URL(uri).openConnection();
return new InputStreamReader(conn.getInputStream());
}
// Retrieve from file.
else
{
return new FileReader(uri);
}
}
}
- 1. innerHTML senza html, solo testo
- 2. Come analizzare HTML in PHP?
- 3. Come analizzare HTML non valido con Perl?
- 4. Come analizzare HTML malformato in python
- 5. jQuery: come ottenere solo testo diretto senza tag (in HTML)
- 6. Come analizzare HTML in DOM con Grunt
- 7. Come analizzare il file html usando clojure?
- 8. analizzare il file HTML locale
- 9. Da destra a sinistra Testo Ingresso HTML
- 10. Come posso estrarre/analizzare i dati tabulari da un file di testo in Perl?
- 11. come convertire il testo HTML in testo normale?
- 12. Estrazione del testo da HTML Java
- 13. Rails come analizzare il testo/event-stream?
- 14. come estrarre il testo principale da html usando Tika
- 15. Recupera testo da file html in java
- 16. Pulizia HTML nel testo da HtmlCleaner
- 17. Come mostrare il testo HTML da API su iPhone?
- 18. Come posso rimuovere HTML da testo in .NET?
- 19. jQuery analizzare HTML senza immagini di carico
- 20. Aggiunta dei HTML-encoded stringa come HTML, non come testo
- 21. Il div HTML contenteditable accetta solo testo in chiaro
- 22. Come accedere a una casella di testo HTML da javascript?
- 23. React.js stampare testo come HTML
- 24. Java: come analizzare doppia da regex
- 25. Linea testo HTML
- 26. Come posso analizzare un file di testo usando javascript
- 27. Come creare solo testo RichTextBox?
- 28. come selezionare solo il testo dell'elemento superiore?
- 29. Crea Great Parser - Estrai testo pertinente da HTML/Blog
- 30. lightbox nessuna immagine solo testo?
come escludere gli elementi invisibili? (ad es. display: nessuno) – Ehsan