Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract text of paragraph from html using Jsoup?

Tags:

jsoup

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JavaApplication14 {


public static void main(String[] args)  {
    try {
        Document doc = Jsoup.connect("tanmoy_mahathir.makes.org/thimble/146").get();  
         String html= "<html><head></head>" + "<body><p>Parsed HTML into a doc."
                 + "</p></body></html>"; 
  Elements paragraphs = doc.select("p");
  for(Element p : paragraphs)
    System.out.println(p.text());
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication14.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

can anyone help me with jsoup code how can i parse just portion including paragraph so that just print

Hello ,World!
Nothing is impossible
like image 636
Tanmoy Mahathir Avatar asked Jun 18 '13 05:06

Tanmoy Mahathir


People also ask

What does jsoup clean do?

clean. Creates a new, clean document, from the original dirty document, containing only elements allowed by the safelist. The original document is not modified. Only elements from the dirty document's body are used.

What is jsoup jsoup?

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors. jsoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do.


1 Answers

For this small bit of html you just need to do

String html= "<html><head></head>" + "<body><p>Parsed HTML into a doc."+
                    +"</p></body></html>"; 
Document doc = Jsoup.parse(html); 
Elements paragraphs = doc.select("p");
for(Element p : paragraphs)
  System.out.println(p.text());

As I see your link contains pretty much the same html you could then also replace the definition of doc with

Document doc = Jsoup.connect("https://tanmoy_mahathir.makes.org/thimble/146").get();

UPDATE

Here is the full code that compiles and runs fine for me.

import java.io.IOException;
import java.util.logging.*;
import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

public class JavaApplication14 {

  public static void main(String[] args)  {
    try {
      String url = "https://tanmoy_mahathir.makes.org/thimble/146";
      Document doc = Jsoup.connect(url).get();
      Elements paragraphs = doc.select("p");
      for(Element p : paragraphs)
        System.out.println(p.text());
    } 
    catch (IOException ex) {
      Logger.getLogger(JavaApplication14.class.getName())
            .log(Level.SEVERE, null, ex);
    }
  }
}
like image 174
selig Avatar answered Oct 16 '22 22:10

selig