Parse HTML in Java code to get the value

Source: Internet
Author: User

Sometimes we get to the page need to parse in the Java code, get the data in the HTML, Jsoup is a very convenient tool.

One, what is jsoup.

Official website: http://jsoup.org/

can download the corresponding jar on the website

Popular will be Jsoup is a page of things to parse

Second, the example

1. The page, through the query to get some data:

2. Source code, this is a table,class= "list", through which to uniquely identify it


3. Code, the HTML is passed in string, using Jsoup to parse:

Import Org.jsoup.Jsoup;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;
        	Public CALSS MyTest {public static void Demo1 (String htmltest) {string html = htmltest;
        	Document doc = jsoup.parse (HTML);
        	Elements rows = Doc.select ("table[class=list]"). Get (0). Select ("tr");
        	if (rows.size () = = 1) {System.out.println ("no result");
            		}else {System.out.println ("---------------------------query result---------------------------");
           	 	Element row = Rows.get (1);
            		System.out.println ("Interim deposit Number:" + row.select ("TD"). Get (0). text ());
            		SYSTEM.OUT.PRINTLN ("Insurance Number:" + row.select ("TD"). Get (1). text ());
            		SYSTEM.OUT.PRINTLN ("policy number:" + row.select ("TD"). Get (2). text ());
            		SYSTEM.OUT.PRINTLN ("Applicant:" + row.select ("TD"). Get (3). text ());
            		SYSTEM.OUT.PRINTLN ("Insured:" + row.select ("TD"). Get (4). text ()); System.out.println ("Number of plates:" + row.Select ("TD"). Get (5). text ());
            		System.out.println ("Frame number:" + row.select ("TD"). Get (6). text ());
            		System.out.println ("Record single:" + Row.select ("TD"). Get (7). text ());
            		System.out.println ("Date of Insurance:" + row.select ("TD"). Get (8). text ());
            		System.out.println ("Deposit Status:" + Row.select ("TD"). Get (9). text ());
            		System.out.println ("Status:" + Row.select ("TD"). Get (). text ());
        	System.out.println ("-----------------------------------------------------------------"); }

    	}
}



4. Output Result:


More ways to read the Jsoup API in detail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.