Jsoup (ii)--Jsoup find DOM elements

Source: Internet
Author: User
Tags tag name

Jsoup methods for finding DOM elements

getElementById (String ID) query DOM based on ID

Getelementsbytag (String tagName) queries the DOM based on the tag name

Getelementsbyclass (String className) querying the DOM based on the style name

Getelementsbyattribute (String key) queries the DOM based on the property name

Getelementsbyattributevalue (String key,string value) queries the DOM based on property name and property value

Second, the Code implementation
     Public Static voidMain (string[] args)throwsexception{//Creating an HttpClient instanceCloseablehttpclient httpClient =Httpclients.createdefault (); //Creating an HttpGet instanceHttpGet HttpGet =NewHttpGet ("http://www.cnblogs.com"); Httpget.setheader ("User-agent", "mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) gecko/20100101 firefox/45.0 "); Closeablehttpresponse Response=Httpclient.execute (HttpGet); String content=NULL; if(Response! =NULL) {httpentity entity=response.getentity (); Content= Entityutils.tostring (Entity, "UTF-8");//get Web page contentDocument Document= Jsoup.parse (content);//parse the Web page to get the Document object                        /*** 1. Get elements based on tag*/Elements Elements= Document.getelementsbytag ("title");//gets the DOM element with the tag titleElement element = Elements.get (0);//gets the first DOM elementString title = Element.text ();//returns the text of an elementSystem.out.println ("The title of the blog Park:" +title); /*** 2. Get elements by ID*/Element Element2= document.getElementById ("Site_nav_top"); String Navtop=Element2.text (); System.out.println ("Motto:" +navtop); /*** 3. Get elements based on style*/Elements elements3= Document.getelementsbyclass ("Post_item"); System.out.println ("============ gets element ============= based on style");  for(Element e:elements3) {System.out.println (e.html ()); System.out.println ("------------------------------"); }                        /*** 4. Querying the DOM based on the attribute name*/Elements elements4= Document.getelementsbyattribute ("width"); System.out.println ("============ query dom============= based on property name");  for(Element e:elements4) {System.out.println (e.tostring ()); System.out.println ("------------------------------"); }                        /*** 5. Querying DOM based on attribute name and property value*/Elements elements5= Document.getelementsbyattributevalue ("target", "_blank"); System.out.println ("============ query dom============= based on property name and property value");  for(Element e:elements5) {System.out.println (e.tostring ()); System.out.println ("------------------------------"); }        }        if(Response! =NULL) {response.close (); }        if(HttpClient! =NULL) {httpclient.close (); }    }

Jsoup (ii)--Jsoup find DOM elements

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.