jsoup--parsing HTML Web page content with Java

Source: Internet
Author: User

When you need to get information from a Web page, you need to parse the HTML page. Filtering the specified label and getting its value is an essential operation, parsing the HTML page this aspect of the tool, Python has beautifulsoup,java has been no good tools, the previous Htmlparser very difficult to use, although can parse, but cannot filter, It is inconvenient to traverse each node from beginning to end, and Jsoup is even more convenient than BeautifulSoup, Jsoup supports a selector syntax similar to CSS (or jquery) for very powerful and flexible lookups.

Here's the basic usage:

1) jsoup.parse (HTML)//creates an HTML text-specified HTML document that returns a Document objectp) Document.select (filter)//filters all elements (or labels) that satisfy a condition by specifying the criteria, returning a elements object that represents a set of element objects. 3) Elements.first ()//returns the first element object of a elements4) Element.text ()//gets the content between the end of a label, such asString HTML= "<div> I am a div</div>";D ocument Doc=jsoup.parse (HTML);d Oc.getelementsbytag ("Div"). First (). text ();//output "I am a div"5) Element.attr (Attrname), gets the value of a property of a label, such as a string HTML= "<div> I am a div</div>";D ocument Doc=jsoup.parse (HTML);d Oc.getelementsbytag ("Div"). The value of first (). attr ("class") is "Divstyle"


Give a comprehensive example:

 Public StaticHashmap<string, object>getdetails (String html) {HashMap<string, object> details =NewHashmap<string, object>(); //parsing beginsDocument doc =jsoup.parse (HTML); //gets the first DIV element that is shaped like <div class= "Result_info ..." >something</div>, Class^=result_info represents class to Result_ Info begins with ^ is the beginning of a regular expressionElement Divall = Doc.select ("Div[class^=result_info]"). First (); //System.out.println (Divall.text ());Element head = Divall.select ("H3"). First ();//get the H3 tag in DivString tvname = Head.select ("a"). attr ("title"). Trim ();//gets the Title property value of the A tag in the H3 tagString year = ""; if(Head.select ("em"). Size () > 0)//If there are multiple <em></em> tags in the H3 tag{ Year= Head.select ("em"). First (). Text (). Trim ();//as long as the first <em> the text between the two} String score= ""; Element scoresection= Divall.select ("P"). First (); if(Scoresection! =NULL) {Elements es= Scoresection.select ("span");//Select a SPAN element            intSize =es.size ();  for(inti = 0; i < size; i++) {Element e=Es.get (i); String content=e.text (). Trim (); Content= Content.replace ("\u00a0", "");//replace &nbsp; to spacesScore + =content; }} HashMap<string, string> lstotherinfo =NewHashmap<string, string>(); Elements othersections= Divall.select ("Div[class^=result_info_cont]");//gets all div elements that satisfy the class beginning with Result_info_cont        intSize = Othersections.size ();//gets the total number of DIV elements that meet the criteria        intInfocount = 0;  for(inti = 0; I < size && Infocount < 3; i++) {String value= ""; Element Item= Othersections.get (i);//get element I            BooleanKeyflag =true; String Key= "";  for(intindex = 0; Index < Item.children (). Size (); index++)//Element.children () to get the immediate child elements of an element{Element e= Item.child (index);//gets the index child element                if(keyflag) {key=e.text (). Trim (); if(Key = = "Introduction")                         Break; Keyflag=false; }                Else                {                    if(E.children (). Size () > 0)                    {                         for(intb = 0; b < E.children (). Size (); b++) {Value+ = E.child (b). Text (). Trim () + ","; }                    }                    Else{String Contents=e.text (). Trim (); Value+ = contents + ","; }}} value= Value.replaceall ("[" + "," + "]$", "" ");            Lstotherinfo.put (key, value); Infocount++; } details.put ("TV", Tvname); Details.put ("Year", year); Details.put ("Score", score); Details.put ("Otherinfo", Lstotherinfo); returndetails; }

Reference Links:

http://jsoup.org/apidocs/

https://www.ibm.com/developerworks/cn/java/j-lo-jsouphtml/

Http://www.open-open.com/jsoup/selector-syntax.htm

jsoup--parsing HTML Web page content with Java

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.