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