How do I convert a document made in Jsoup (the Jav

2019-03-17 13:33发布

I have a document that was made in jsoup that looks like this

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();

How do i convert that doc into a string.

3条回答
The star\"
2楼-- · 2019-03-17 13:50
 Document doc = Jsoup.connect("http://en.wikipedia.org/").get();     
 Elements post = doc.select("div.post-content");
 String dd = post.toString();
 Document ffffd = Jsoup.parse(dd);

After parsing the string to document then you can use on it document functions

 Elements scriptTag = ffffd.getElementsByTag("script");
 System.out.println(scriptTag);
查看更多
小情绪 Triste *
3楼-- · 2019-03-17 14:08

doc.toString() works, as does doc.outerHtml().

查看更多
4楼-- · 2019-03-17 14:13

Have you tried:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
String htmlString = doc.toString();

As Document extends Element it also has got the method html() which "Retrieves the element's inner HTML" according to the API. So that should work:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
String htmlString = doc.html();

Additional Info:

Each Document object has got a reference to an instance of the inner class Document.OutputSettings which can be accessed via the method outputSettings() of Document. There you can enable/disable pretty-printing by using the setter prettyPrint(true/false). See the API for Document and Document.OutputSettings for furtherinformation

查看更多
登录 后发表回答