After parsing a Document Instance Object and finding some elements, you want to obtain the data in these elements. To obtain an attribute value, you can use Node. attr (String key) values () or Node. outerHtml () as an example:
String html = "<p> An <a href = 'HTTP: // example.com/'> <B> example </B> </a> link. </p> "; Document doc = Jsoup. parse (html); // parse the HTML string and return a Document to implement Element link = doc. select (""). first (); // search for the first a element String text = doc. body (). text (); // "An example link" // obtain the text String linkHref = link in the String. attr ("href"); // "http://example.com/" // get link address String linkText = link. text (); // "example" "// obtain the text String linkOuterH = link in the link address. outerHtml (); // "<a href =" http://example.com "> <B> example </B> </a>" String linkInnerH = link.html (); // "<B> example </B>" // obtain the html content in the link.
The preceding method is the core method for element data access. In addition, you can use Element. id () Element. tagName () Element. className () and Element. hasClass (String className) All these accessors have corresponding setter methods to change data.