Book Image

Instant jsoup How-to

By : Pete Houston
Book Image

Instant jsoup How-to

By: Pete Houston

Overview of this book

As you might know, there are a lot of Java libraries that support parsing HTML content out there. Jsoup is yet another HTML parsing library, but it provides a lot of functionalities and boasts much more interesting features when compared to others. Give it a try, and you will see the difference! Instant jsoup How-to provides simple and detailed instructions on how to use the Jsoup library to manipulate HTML content to suit your needs. You will learn the basic aspects of data crawling, as well as the various concepts of Jsoup so you can make the best use of the library to achieve your goals. Instant jsoup How-to will help you learn step-by-step using real-world, practical problems. You will begin by learning several basic topics, such as getting input from a URL, a file, or a string, as well as making use of DOM navigation to search for data. You will then move on to some advanced topics like how to use the CSS selector and how to clean dirty HTML data. HTML data is not always safe, and because of that, you will learn how to sanitize the dirty documents to prevent further XSS attacks. Instant jsoup How-to is a book for every Java developer who wants to learn HTML manipulation quickly and effectively. This book includes the sample source code for you to refer to with a detailed explanation of every feature of the library.
Table of Contents (7 chapters)

Miscellaneous Jsoup options (Should know)

Usually, developers only work on Jsoup with default options, unaware that it provides various useful options. This recipe will acquaint you with some common-use options.

How to do it...

  1. How to work with connection objects:

    • Setting userAgent: It is very important to always specify userAgent when sending HTTP requests. What if the web page displays some information differently on different browsers? The result of parsing might be different.

      Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1)").get();

    Especially when using Jsoup in Android, you must always specify a user agent; otherwise, it won't work properly.

    • When forced to work with different content types:

      Document doc = Jsoup.connect(url).ignoreContentType(true).get();

    By default, Jsoup only allows working with HTML and XML content type and throws exceptions for others. So, you will need to specify this properly in order to work with other content types, such as RSS, Atom, and so on.

    • Configure a connection timeout:

      Document doc = Jsoup.connect(url).timeout(5000).get();

    The default timeout for Jsoup is 3000 milliseconds (three seconds). Zero indicates an infinite timeout.

    • Add a parameter request to the connection:

      Document doc = Jsoup.connect(url).data("author", "Pete Houston").get();

    In dynamic web, you need to specify a parameter to make a request; the data() method works for this purpose.


    Please refer to the following link for more information:, java.lang.String)

    • Sometimes, the request is post.

      Document doc = Jsoup.connect(url).data("author", "Pete Houston").post();
  2. Setting the HTML output of the Document class.

    This option works through the Document.OutputSettings class.


    Please refer to the following link for more information:

    This class outputs HTML text in a neat format with the following options:

    • Character set: Get/set document charset

    • Escape mode: Get/set escape mode of HTML output

    • Indentation: Get/set indent amount for pretty printing (by space count)

    • Outline: Enable/disable HTML outline mode

    • Pretty print: Enable/disable pretty printing mode

    For example, display the HTML output with; charset as utf-8 and the indentation amount as four spaces, enable the HTML outline mode, and enable pretty printing:

    Document.OutputSettings settings = new Document.OutputSettings();
    Document doc = …// create DOM object somewhere.doc.outputSettings(settings);

    After setting the output format to Document, the content of Document is processed into the according format; call the Document.html()method for output result.

  3. Configure the parser type.

    Jsoup provides two parser types: HTML parser and XML parser.

    By default, it uses HTML parser. However, if you are going to parse XML such as RSS or Atom, you should change the parser type to XML parser or it will not work properly.

    Document doc = Jsoup.connect(url).parser(Parser.xmlParser()).get();

There's more...

The previously mentioned options in Jsoup are important ones that the developers should know and make use of.

However, there are several more that you can try: