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